episkeyyy episkeyyy - 6 months ago 14
MySQL Question

Keep query in search bar, but use placeholder for initial default value

I have a search bar that returns matching data according to what I type. To make it user-friendly I made the input value keep the search query in the bar even after pressing submit. However, when I initially load my page without having done any searches, "null" appears instead of my placeholder. How do I make my placeholder the default value when first running the page? I also have a NullPointerException when I run my project and am wondering if that may be related.

Says null upon initial start, doesn't use placeholder

Here is the search bar code:

<form method="post" id="form1" action="index.jsp">

Search <input type="text" name="searchRequest"
placeholder="Enter your search here..."
value="<%=request.getParameter("search")%>" /> <input type="submit"
id="button1" value="Search" name="submit" />


</form>

Answer

One way to do this is you can check for null first. To do that download jstl-1.2.jar file, put it in WEB-INF/lib and modified your jsp with following code.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> // add at start of jsp page


<form method="post" id="form1" action="index.jsp">
    Search <input type="text" name="searchRequest" placeholder="Enter your search here..." <c:if test="${request.getParameter != null}">value="<%=request.getParameter("search")%>"</c:if> /> 
    <input type="submit" id="button1" value="Search" name="submit" />
</form>