JayDude Posted March 27, 2012 Share Posted March 27, 2012 I created a drop-down menu using a MySQL statement in php for a form. My drop down menu works fine, but I want to assign a default value to it (normal text) the value will never change, thus I do not need to extract the default value from the table, I already know the value. Here is the basic snippet from the script: <?php include("../includes/xxx.php"); $cxn = mysqli_connect($host,$user,$password,$dbname); $query = "SELECT DISTINCT `plant_id` FROM `plant` ORDER BY `plant_id`"; $result = mysqli_query($cxn,$query); while($row = mysqli_fetch_assoc($result)) { extract($row); echo "<option value='$plant_id'>$plant_id</option>\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/259834-assign-a-default-value-in-a-field-with-a-sql-select-query-with-php/ Share on other sites More sharing options...
cpd Posted March 27, 2012 Share Posted March 27, 2012 <?php include("../includes/xxx.php"); $cxn = mysqli_connect($host,$user,$password,$dbname); $query = "SELECT DISTINCT `plant_id` FROM `plant` ORDER BY `plant_id`"; $result = mysqli_query($cxn,$query); while($row = mysqli_fetch_assoc($result)) { extract($row); $selected = ($plant_id == EXPECTED_ID ? "selected='selected'" : ""); echo "<option value='$plant_id' {$selected}>$plant_id</option>\n"; } ?> Obviously replace the EXPECTED_ID with whatever your selected plant ID should be. Link to comment https://forums.phpfreaks.com/topic/259834-assign-a-default-value-in-a-field-with-a-sql-select-query-with-php/#findComment-1331700 Share on other sites More sharing options...
JayDude Posted March 27, 2012 Author Share Posted March 27, 2012 I tried that, but now my dropdown list gives an empty field with a blank dropdown list. Here is the snippet you gave me with the altered field:- (note I used ' ' else I get an error) { extract($row); $selected = ($plant_id == 'Blue' ? "selected='selected'" : ""); echo "<option value='$plant_id' {$selected}>$plant_id</option>\n"; } Link to comment https://forums.phpfreaks.com/topic/259834-assign-a-default-value-in-a-field-with-a-sql-select-query-with-php/#findComment-1331712 Share on other sites More sharing options...
JayDude Posted March 27, 2012 Author Share Posted March 27, 2012 CPD, Ignore my last post, it is working fine, thanks for the help... But I now have another question for you, maybe a bit more challenging. I have another dropdown list in the same form, script looks exactly like my first (and working fine) but this dropdown menu must only show items from the table corresponding with the first selected field. I have seen this in carsales websites so I know it can be done. Please help Link to comment https://forums.phpfreaks.com/topic/259834-assign-a-default-value-in-a-field-with-a-sql-select-query-with-php/#findComment-1331722 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.