Help with select/option and forms

Soldato
Joined
1 Mar 2003
Posts
5,508
Location
Cotham, Bristol
Hi Guys,

I'm trying to set a value via the select/option drop downs in php. I have something very similar working for buttons but I can't get this to work, it does nothing.

cart.php
PHP:
<?php
   $delivery_option = 6;
   if(isset($_GET['delivery'])) {
      switch($_GET['delivery']) {
         case "royal":
            $delivery_option = 6;
            break;
         case "free":
            $delivery_option = 0;
            break;
      }
   }
   echo "<form action=\"cart.php\" method=\"get\">\n";
   echo "   <select name=\"delivery\">\n";
   echo "      <option value=\"royal\">Royal Mail - &pound;6</option>
   echo "      <option value=\"free\">Local Delivery - &pound;0</option>
   echo "   </select>\n";
   echo "</form>\n";
   echo "<p>Delivery Price: &pound;$delivery_option</p>\n";
?>

Now what I'm expecting to happen is that the page reloads with the new delivery value when the option is changed, but nowt happens. I can get buttons to work using this method and setting hidden variables, so I don't understand why this doesn't work?

edit: I've tried with method=post and $_POST['delivery'] as well
 
Last edited:
Not really my question, I wanted to know how to get the form to do it's action after I changed the option in the select. Now I've found how to do this, problem is after changing the option it always selects the same option

Here's the code

PHP:
<?php
   $delivery_option = 6;
   if(isset($_GET['delivery'])) {
      switch($_GET['delivery']) {
         case "royal":
            $delivery_option = 6;
            break;
         case "free":
            $delivery_option = 0;
            break;
      }
   }
   echo "<form action=\"cart.php\" method=\"get\" name=\"deliveryFrom\">\n";
   echo "   <select name=\"delivery\" onChange=\"document.forms['deliveryForm'].submit();\">\n";
   echo "      <option value=\"royal\">Royal Mail - &pound;6</option>\n";
   echo "      <option value="free\">Local Delivery - &pound;0</option>\n";
   echo "   </select>\n";
   echo "</form>\n";
   echo "<p>Delivery Price: &pound;$delivery_option</p>\n";
?>
 
Marc2003 explained how to do this.

On the load of the page you need to check the value of the select passed in the GET collection. Then you need to compare it to each item of the select. If the values match add selected="selected" to the options' parameters.
 
Marc2003 explained how to do this.

On the load of the page you need to check the value of the select passed in the GET collection. Then you need to compare it to each item of the select. If the values match add selected="selected" to the options' parameters.

Ah right yes, obvious now.
 
Back
Top Bottom