Crazy Horse
asked on
stop form action from going to page if search field is empty
I have a search field in a form that looks like this on my index.php page.
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:
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?
<form action="search.php" method="post">
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";
}
}
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?
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.
ASKER
Thanks Ray. But "required" doesn't work with safari, I need something that does.
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'.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Works like a charm!
http://www.w3schools.com/tags/att_input_required.asp