boney alex Posted April 18, 2007 Share Posted April 18, 2007 Hi, I making an 'Add New Vehicle' feature using drop down lists for some of the data entry. If there is a probelm with the data entry such as a null field then the form is shown with all the previous entered data. How do I show what option has already been selected in a drop down list? newvan.php <form name="newvan" action="addnewvehicle.php" method="post"> <fieldset> <legend>Vehicle Information</legend> <label for="registration">Registration:</label> <input type="text" input id="registration" name="registration" size="10"><br><br> <?php $result = mysql_query("SELECT MakeModelID, Make, Model, Specification FROM MakeandModel ORDER BY Make, Model, Specification"); ?> <label for="branch">Type of Van:</label> <select name="vantype" STYLE="width: 220px" size="0"> <option selected value=""></option> <?php while ($row = mysql_fetch_array($result)) { echo "<option value=\"{$row[MakeModelID]}\">{$row[Make]} {$row[Model]} {$row[specification]}</option>\n"; } ?> </select><br><br> <?php $result = mysql_query("SELECT BranchID, Branch_Name FROM Branch ORDER BY Branch_Name"); ?> <label for="branch">Branch:</label> <select name="branch" STYLE="width: 220px" size="0"> <option selected value=""></option> <?php while ($row = mysql_fetch_array($result)) { echo "<option value=\"{$row[branchID]}\">{$row[branch_Name]}</option>\n"; } ?> </select><br><br> <label for="colour">Colour:</label> <input type="text" input id="colour" name="colour" size="20"><br><br> <label for="mileage">Mileage (e.g. 55000):</label> <input type="text" input id="mileage" name="mileage" size="10"><br><br> <label for="transmission">Transmission:</label> <select name="transmission" STYLE="width: 90px" size="0"> <option selected value=""></option> <option value="Manual">Manual</option> <option value="Auto">Automatic</option> </select><br> </fieldset> <br> <center><input type="submit" value= "Add Vehicle" class="button" name="submit"></center> </form> I can show the normal text fields by POSTING the value into a variable and echo in the value: <label for="registration">Registration:</label> <input type="text" input id="registration" name="registration" size="10" value="<? echo $registration; ?>"><br><br> <label for="colour">Colour:</label> <input type="text" input id="colour" name="colour" size="20" value="<? echo $colour; ?>"><br><br> <label for="mileage">Mileage (e.g. 55000):</label> <input type="text" input id="mileage" name="mileage" size="10" value="<? echo $mileage; ?>"><br><br> Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/47537-solved-drop-down-problem/ Share on other sites More sharing options...
marmite Posted April 18, 2007 Share Posted April 18, 2007 Hey, not sure if this is what you mean (you want to keep the same selections between pages?) The below works for me... <form action="/greetingscards.php" method="post" name = "themes" id="themes"> <table width="90%"> <tr width="100%">Theme</tr> <tr> <td width="50px"><select name="theme" id="theme1"> <option value="all"<?php if($_POST['theme'] == "all") { echo ' selected="selected"'; } ?>>All</option> <option value="animal"<?php if($_POST['theme'] == "animal") { echo ' selected="selected"'; } ?>>Animal</option> <option value="floral"<?php if($_POST['theme'] == "floral") { echo ' selected="selected"'; } ?>>Floral</option> <option value="leisure"<?php if($_POST['theme'] == "leisure") { echo ' selected="selected"'; } ?>>Leisure</option> <option value="travel"<?php if($_POST['theme'] == "travel") { echo ' selected="selected"'; } ?>>Travel</option> </select></td> <td><input type="submit" name="submit3" value="Find cards!" /></td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/47537-solved-drop-down-problem/#findComment-232059 Share on other sites More sharing options...
boney alex Posted April 18, 2007 Author Share Posted April 18, 2007 thanks mate, that works great for my HTML populated drop down, BUT how would you go about keepin selections if the drop down list was populated from a table in your MySQL database??????? For example: <?php $result = mysql_query("SELECT BranchID, Branch_Name FROM Branch ORDER BY Branch_Name"); ?> <label for="branch">Branch:</label> <select name="branch" STYLE="width: 220px" size="0"> <option selected value=""></option> <?php while ($row = mysql_fetch_array($result)) { echo "<option value=\"{$row[branchID]}\">{$row[branch_Name]}</option>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47537-solved-drop-down-problem/#findComment-232094 Share on other sites More sharing options...
mpharo Posted April 18, 2007 Share Posted April 18, 2007 If I am getting it right this is what you would do like the previous post has but you would compare the sql result to the value in your option...something like this... <select> <option value="TEST" <? If ($sql[result]=='TEST') { echo "selected"; }?>>TEST</option> </select> It is much cleaner if your not breaking in and out of php to html and just use an echo statement to generate the html... Quote Link to comment https://forums.phpfreaks.com/topic/47537-solved-drop-down-problem/#findComment-232136 Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 like this: <?php while ($row = mysql_fetch_array($result)) { echo "<option value=\"{$row[branchID]}\"". (($_POST['branch'] == $row['BrandID']) ? (' SELECTED') : ('')) .">{$row[branch_Name]}</option>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/47537-solved-drop-down-problem/#findComment-232138 Share on other sites More sharing options...
boney alex Posted April 18, 2007 Author Share Posted April 18, 2007 Cheers boo_lolly, works a threat! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/47537-solved-drop-down-problem/#findComment-232150 Share on other sites More sharing options...
boo_lolly Posted April 18, 2007 Share Posted April 18, 2007 Cheers boo_lolly, works a threat! Thanks! glad i could help =) don't forget to click 'Topic Solved', please. Quote Link to comment https://forums.phpfreaks.com/topic/47537-solved-drop-down-problem/#findComment-232157 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.