I am trying to create a very primal search function using php/sql.
Basically, I am using the GET method to submit the form query over several fields in /search.php.
So the query will be transmitted in the following form:
clothes.php?type=sometype&color=somecolor&mat=somemat&pat=somepattern&uprc=price&brand=brand
However, I have scripted clothes.php such that it all fields do not have to be filled for a successful query.
clothes.php?type=sometype
will return clothes of the specified type.
For all my fields in search.php
, I am using (select) and (option) to list the available options under each field.
The problem is, in /search.php
, I want to prevent search.php from sending form data if a particular field is empty.
I tried creating <select name="type"><option value = ""></option>...</select>
as the first option in each dropdown box but it still sends some data under that field.
Is there a workaround for this?
The best you can do using pure HTML is to submit empty strings (""
) for empty values, e.g. foo=&bar=baz
. null
is not possible either way, HTTP has no notion of null
values. If you want to remove the empty foo
from this query string, you'd have to do so using Javascript on the client when the form is submitted. Since there are always clients without Javascript though, there'll always be cases were you receive an empty foo
, so you need to be prepared for it anyway.
If you insist on "clean" URLs no matter what, you could redirect on the server and filter out empty parameters. E.g. submit the search form via POST, then build a GET URL with the non-empty parameters and redirect to it. This'll have the overhead of an additional HTTP request though.
I don't think there's a way to prevent an HTML form from sending an empty data item for empty form fields short of hooking the submit event in javascript and creating the form post by hand.
You can't stop a user from sending you data, especially not GET
data like in your example.
You could try adding an onclick
handler to your submit button or something, but your users could have JavaScript disabled. Even if they had JavaScript enabled, there's nothing from stopping someone from going to their address bar and manually typing www.example.com/file.php?var=value
. Hell, even if you change your method from GET
to POST, there are still ways to submit data without using your page.
The only thing you can really do is to check if the field has a value and, if it does, redirect the user back to your search page with an error telling them to try again.
if (!isset($_GET['field']) || empty($_GET['field])) {
// regenerate page with error
}
Your title is inaccurate as well. You'll never get null data from a form. As far as PHP is concerned, either the variable is included or it's not. If it's included, then the worst thing it can be is blank. For example www.example.com/file.php?field
results in a $_GET['field']
value that is empty, not null.
There are some types - like radio buttons and checkboxes - that won't send values if they're empty, but PHP wouldn't create a $_GET
value for them at all. They would simply fail an isset()
check, rather than having a null
value.
You would have to use a PHP conditional statement like this:
if($_GET['field_name'] != NULL) {
// do sql stuff here
}
else {
echo "Please select a field";
}
Where I have "NULL" you can enter the default option value your select menus have.