0

I would like to include a feature that lets users of my site sort the results of a search, either by name or by price.

This is fairly simple as all i intend on doing is changing the mysql query, via php.

So basically the query looks like this:

$searchResultQuery = "
    SELECT id, name, price
    FROM items
    WHERE MATCH (name)
    AGAINST ('{$GLOBALS['input']}' IN BOOLEAN MODE)
    ORDER BY $GLOBALS['sort']
";

And the html looks something like this:

<form>
<select name="sort" id="sort" style="float: left;">
    <option value="name">Name</option>
    <option value="price">Price</option>
</select>
</form>

What i want to do, is have the form automatically submit when a new option is selection. As in, by default the results are ordered by name, but if the user chooses price, then it will adjust the results so display in order of their prices. This would happen without the user needing to click on a submit button of some sort.

As i have a very bleak knowledge of javascript, I have been reading up on some of the html5 additions, and it seems that there is nothing to cover this. Is this possible without the use of javascript or am i forced to learn how to use the onchange attribute?

If anyone has any input regarding this, it would be greatly appreciated, thank you!

EDIT:

This would be what i have added to the controller, although i assume this is the reason that is is not being passed:

if (isset($_POST['sort']))
{   
    $GLOBALS['sort'] = $_POST['sort'];
}

How would i add the forms value to the globals variable?

3
  • 2
    Are you confusing Java and Javascript? Commented Dec 26, 2011 at 8:57
  • @Sergei Tulentsev No, there is no confusion, just a problem with my wording that has now been corrected, thanks! Commented Dec 26, 2011 at 10:02
  • WARNING! The accepted answer is essentially insecure and leads straight to SQL injection! Refer to the answer linked at the top for the proper solution Commented Mar 3, 2017 at 5:50

2 Answers 2

2

Unfortunatly, you cannot submit another query via php without reloading the page. You will have to use javascript. Ajax is what you're looking for

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

Comments

1

Here is the PHP code (don't forget to escape user input before sending it to DB engine):

$pattern = mysql_real_escape_string($GLOBALS['input']);
$sort = mysql_real_escape_string(isset($GLOBALS['sort']) ? $GLOBALS['sort'] : 'name');

$searchResultQuery = "
    SELECT id, name, price
    FROM items
    WHERE MATCH (name)
    AGAINST ('$pattern' IN BOOLEAN MODE)
    ORDER BY $sort
";

And to automatically submit the form you have to handle select's onchange event:

<form name="myform">
<select name="sort" id="sort" style="float: left;" onChange="javascript:document.myform.submit();">
    <option value="name">Name</option>
    <option value="price">Price</option>
</select>
</form>

11 Comments

Thank you for your input, that seems like a simple enough approach, but when applying your suggestion, i am faced with: Notice: Undefined index: sort in C:\wamp\www\domain\searchResults.html.php on line 29 Any Ideas what could be causing this?
Check your $searchResultQuery to see the resulting SQL query; put this after your $searchResultQuery = "..." string: var_dump($searchResultQuery);
I'm sorry, I've misprinted in $sort definition. Please look at my initial reply, I fixed the second line $sort = ... (instead of $GLOBALS['sort'] = ...)
That means that the element with index sort is missing in the $GLOBALS array. Check that you've put it there before this code.
Yes, your edits will set the $GLOBALS['sort'] when you pass some value to the script. I updated my code to handle undefined $GLOBALS['sort'] more correctly. You should do something similar to $GLOBALS['input']. And in general it is not the best practice to use $GLOBALS extensively. Passing parameters to your function or using OOP is much better.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.