Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/47537-solved-drop-down-problem/
Share on other sites

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>

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";
} 
?>

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.