Solved

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

Posted on 2016-09-08
7
25 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 108

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 108

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
 

Author Comment

by:Black Sulfur
ID: 41790095
Thanks Ray. But "required" doesn't work with safari, I need something that does.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 82

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 82

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 108

Accepted Solution

by:
Ray Paseur earned 500 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
 

Author Closing Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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 …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now