Solved

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

Posted on 2016-09-08
7
32 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 109

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 109

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 Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 83

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 83

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 109

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

829 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