PHP Search query help
I have the following code:
<?php
function search_reset()
{
$query = $_GET['q'];
if($query)
echo '<a class="reset" title="Clear search query" href="http://localhost:8888/search/">Clear search query</a>';
}
function search_query()
{
$query = $_GET['q'];
echo $query;
}
?>
<form method="get" action="">
<fieldset>
<input type="text" name="q" id="q" value="<?php search_query(); ?>" placeholder="Search" />
<?php search_reset(); ?>
<span class="submit">
<input type="submit" name="submit" id="sub开发者_如何学JAVAmit" value="Search" title="Search" />
</span>
</fieldset>
</form>
The idea is that when a user does a search it will do the normal query string e.g. domain.com/?q=searchquery
but instead it adds a second query string from the submit button like so: /?q=hello&submit=Search
how do I stop this? I don't remember ever having this problem before :/
Thanks
The submit button is a form element to be submitted as well. Remove the name off the element and it won't be submitted with the rest of the form elements.
The query is composed from form elements, by pairing up the name and value of each. So if you have form elements named "drink", "milk", "sugar", and the user types values "tea", "yes", "3", then the query string is (with spaces for clarity):
mysite.com ? drink=tea & milk=yes & sugar=3
So in your form, you have to set values so that the query string is rebuilt:
<form method="get" action="">
<input type="text" name="q" id="q" value="<?php search_query(); ?>" placeholder="Search" />
<?php search_reset(); ?>
<input type="submit" id="submit" title="Search" />
</form>
精彩评论