0

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="submit" 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

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.