j05hr Posted June 21, 2010 Share Posted June 21, 2010 I've got a dropdown box and when you submit the value, the drop down resets itself. http://www.bulletproofwebdesign.co.uk/orderform/calculator.php <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <option value= "100" <?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>>100</option> <option value= "250" <?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>>250</option> <option value= "500" <?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>>500</option> </select> </p> <p>Double sided <INPUT TYPE="radio" NAME="q1" VALUE="yes"> Single Sided <INPUT TYPE="radio" NAME="q1" VALUE="no"> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> Is there a way I can make it not reset after you change the value? Thanks, Josh Link to comment https://forums.phpfreaks.com/topic/205433-drop-down-box/ Share on other sites More sharing options...
joePHP Posted June 21, 2010 Share Posted June 21, 2010 Try this: <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <option value= "100" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 100) echo 'selected="selected"'; ?>>100</option> <option value= "250" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 250) echo 'selected="selected"'; ?>>250</option> <option value= "500" <?php if (isset($_POST['quantity']) && $_POST['quantity'] == 500) echo 'selected="selected"'; ?>>500</option> </select> </p> <p>Double sided <INPUT TYPE="radio" NAME="q1" VALUE="yes"> Single Sided <INPUT TYPE="radio" NAME="q1" VALUE="no"> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> Joe Link to comment https://forums.phpfreaks.com/topic/205433-drop-down-box/#findComment-1075075 Share on other sites More sharing options...
j05hr Posted June 21, 2010 Author Share Posted June 21, 2010 Thanks Joe Link to comment https://forums.phpfreaks.com/topic/205433-drop-down-box/#findComment-1075078 Share on other sites More sharing options...
kratsg Posted June 21, 2010 Share Posted June 21, 2010 Actually, you don't need to check if it's set if you're only looking for ONE specific value of it... <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <option value= "100" <?php echo ($_POST['quantity'] == 100)?'selected="selected"':''; ?>>100</option> <option value= "250" <?php echo ($_POST['quantity'] == 250)?'selected="selected"':''; ?>>250</option> <option value= "500" <?php echo ($_POST['quantity'] == 500)?'selected="selected"':''; ?>>500</option> </select> </p> <p>Double sided <INPUT TYPE="radio" NAME="q1" VALUE="yes"> Single Sided <INPUT TYPE="radio" NAME="q1" VALUE="no"> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> Or better yet, if there's a lot of values: <form action="calculator.php" method="post"> <p>Quantity: <select name="quantity"> <?php $possible_vals = array(100,250,500); foreach($possible_vals as $value){ echo "<option value= '$value'"; if($value == $_POST['quantity']){ echo ' selected=\'selected\''; } echo ">$value</option>"; } ?> </select> </p> <p>Double sided <INPUT TYPE="radio" NAME="q1" VALUE="yes"> Single Sided <INPUT TYPE="radio" NAME="q1" VALUE="no"> <p>Total: £<?php echo $sum?></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> Link to comment https://forums.phpfreaks.com/topic/205433-drop-down-box/#findComment-1075080 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.