Home Ask Login Register

Developers Planet

Your answer is one click away!

pouya February 2016

Select field resets when page is reset

i write a code which user can select an option in select field. and it have onchange="location = this.options[this.selectedIndex].value;" on select tag and every option tag have own value.

So my question is this:

when i select an option from this select field, it send query string to same page and i want to my selected option keep selected but select field is reset when page reset. it's wasn't such and was work true, but i think i make some change in my code (which i can't find that) and that is make problem.

here is my code:



                    require ('../config.php');
                     /* connect and select db codes*/

                    $dbresult=mysqli_query($connection, "SELECT * FROM services");

                    while($row = mysqli_fetch_array($dbresult)){
                            $order_name = $row['name'];
                            $order_id = $row['id'];
                            echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\">$order_name</option>";


  /* ...continue of my codes...*/


user5697101 February 2016

You have to specify that the option is selected in an attribute. So for the option that you want to select, you need to add the selected="selected" attribute

In PHP this could be done with something like this before your echo statement, it will select this option if the order id is the same as the order id specified in the query string

  if($service == $order_id){
     $selected = "selected";
     $selected = "";

and in your echo statement

   echo "<option value=\"NewOrder.php?service=$order_id&quantity=1th\" ".$selected.">$order_name</option>";

Post Status

Asked in February 2016
Viewed 1,005 times
Voted 5
Answered 1 times


Leave an answer

Quote of the day: live life