herghost Posted March 12, 2010 Share Posted March 12, 2010 Hi all, I am working on an update form, all good apart from one thing. how do you show the selected option from a drop down list? For everything else I have just been echoing the value, but confused about this! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/194980-echo-select-statement/ Share on other sites More sharing options...
teamatomic Posted March 12, 2010 Share Posted March 12, 2010 $drop=$_POST['dropdown']; echo "$drop"; HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/194980-echo-select-statement/#findComment-1025077 Share on other sites More sharing options...
Grayda Posted March 12, 2010 Share Posted March 12, 2010 A simplistic way to go about this would be: <select> <?php for($i = 0; $i <= count($choice) - 1; $i++) { echo "<option"; if($_POST["myChoice"] == $choice[$i]) { echo " selected='selected'"; } echo ">" . $_POST["myChoice"] . "</option>"; } ?> </select> Keep in mind that this is very simplistic, untested (I'm at work :B) and is probably not secure (unsanitized $_POST data just asking for an XSS Attack) but you get the idea. You create part of your HTML tag then add an if() statement. If the data in $_POST is equal to the choice being outputted, add the "selected='selected'" property to the tag. If the data doesn't match, it finishes off the HTML tag and carries on. With some tweakage, you can use this to take information directly from a database and automatically select options based on row data. But be careful. If you don't sanitize user input, you leave yourself wide open to an XSS attack or much worse. Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/194980-echo-select-statement/#findComment-1025078 Share on other sites More sharing options...
herghost Posted March 12, 2010 Author Share Posted March 12, 2010 Thanks to both of you, however I am having some problems getting my head around it, I have posted my code so you can see how I am getting my data from the database etc, The last block is the one I am working on (cat_show) Basically in the database is just 1 for yes or 0 for no: <?php $catid = $_GET['id']; $query = "SELECT * FROM product_category WHERE catid = '$catid'"; $result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR); // while there are rows to be fetched... while ($e = mysql_fetch_assoc($result)) { ?> <div id="body-container"> <div id="body"><div id="container1"> <form action="formactions/updatecat.php" method="post" class="niceform"> <fieldset> <legend>Add Product Category</legend> <dl> <dt><label for="cat_name">Category Name:</label></dt> <dd><input type="text" name="cat_name" id="cat_name" size="32" maxlength="32" value="<?php echo $e['cat_name'];?>" /> </dd> <script> var cat_name = new LiveValidation('cat_name'); cat_name.add( Validate.Presence ); </script> </dl> <dl> <dt><label for="cat_desc">Category Description:</label></dt> <dd><textarea name="cat_desc" cols="32" rows="5" id="cat_desc"><?php echo $e['cat_desc'];?></textarea> </dd> <script> var cat_desc = new LiveValidation('cat_desc'); cat_desc.add( Validate.Presence ); </script> </dl> <dt><label for="cat_show">Show to Customer?:</label></dt> <dd><select size="1" name="cat_show" id="cat_show"> <option value="1">Yes</option> <option value="2">No</option> </select> </dd> </dl> </fieldset> <fieldset class="action"> <input type="submit" name="submit" id="submit" value="Update Category" /> </fieldset> </form> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/194980-echo-select-statement/#findComment-1025079 Share on other sites More sharing options...
PravinS Posted March 12, 2010 Share Posted March 12, 2010 Use like this <select size="1" name="cat_show" id="cat_show"> <option value="1" <?php if($e['cat_show'] == 1) echo " selected ";?>>Yes</option> <option value="2" <?php if($e['cat_show'] == 0) echo " selected ";?>>No</option> </select> Quote Link to comment https://forums.phpfreaks.com/topic/194980-echo-select-statement/#findComment-1025083 Share on other sites More sharing options...
herghost Posted March 12, 2010 Author Share Posted March 12, 2010 Thanks That did it Quote Link to comment https://forums.phpfreaks.com/topic/194980-echo-select-statement/#findComment-1025087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.