Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

stop form action from going to page if search field is empty

Posted on 2016-09-08
7
Medium Priority
?
40 Views
Last Modified: 2016-09-08
I have a search field in a form that looks like this on my index.php page.

 
 <form action="search.php" method="post">

Open in new window


The search function is working fine but I don't want the page to actually go to search.php if the search field is empty i.e.: they just click "search" without actually inputting anything.

I tried this above the form:

 $error = "";
		  
		  if($_SERVER['REQUEST_METHOD'] == "POST"){
			  
			  if (empty($_POST['search'])){
				  
				  $error .="Please enter a search term";
				  
			  } 
		  }

Open in new window


But it still posts the search.php page and shows the error there. Is it possible to stop the page from redirecting to search.php if the search input is empty?
0
Comment
Question by:Black Sulfur
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41790073
Yes, it's possible, but this is not a PHP question.  You use JavaScript to influence behaviors in the client browser.  You might try using the "required" attribute in the search field.
http://www.w3schools.com/tags/att_input_required.asp
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 41790085
Also, you might rethink the use of a POST request for a search.  If you use GET instead of POST, your clients can bookmark the page, they can share the link with others, etc.  The search parameters are in the URL.  It's just a much more user-friendly way to work the issue.
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41790095
Thanks Ray. But "required" doesn't work with safari, I need something that does.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41790098
I use POST for my search functions and filter the values like crazy.  A 'Search' form is a Big target for spammers.  Too often the 'users' aren't 'user friendly'.
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 41790104
You have to do it twice in any case.  Use Javascript in the browser and PHP on the server.  Remember that spammers will bypass your form and send directly to your 'action' page.
1
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 41790108
Try this.  I don't have an instance of Safari to test, but it seems like it should work.
http://www.html5rocks.com/en/tutorials/forms/constraintvalidation/#toc-safari

Also, whether the request is GET or POST, all external input (GET, POST, COOKIES, Daatabase, etc) is by definition tainted and therefore must be considered an attack vector.  Always filter everything!
1
 
LVL 1

Author Closing Comment

by:Black Sulfur
ID: 41790159
Works like a charm!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question