Jump to content

Dynamic Drop Down Box


nealios
 Share

Recommended Posts

I have a dynamic drop down box that populates with customer names from my database.This works correctly and communicates with the database and i can update my job table once a customer has been chosen/allocated to each job.However when i come out and go back into the form although the customer information is still in the database the drop down obviously defaults back to the first customer in the database.Is there a way in which the drop down box can default to the value that was previously selected which is stored in the database?Here is my attempt that is currently only defaulting the drop down to the title customer name.

	$sql = mysql_query("SELECT `cid`, `first_name`, `surname` FROM `customer`			ORDER BY `surname`			ASC");	$a = 1;	$customer_select .= "<select name=\"cid\" onchange=\"showCustomer(this.value)\">";	if ($name == 0) {	$selected = "selected";	} else {	$selected = "";	}	$customer_select .= "<option value=\"0\" $selected>Customer Name</option>";	while(list($cid, $firstname, $surname) = mysql_fetch_row($sql)) {	if ($cid == $name) {		$selected = "selected";	} else {		$selected = "";	}	$customer_select .= "<option value=\"$cid\" $selected>$firstname $surname</option><br>";	$a++;	}	$customer_select .= "</select>";

Many thanks

Link to comment
Share on other sites

Is there a way in which the drop down box can default to the value that was previously selected which is stored in the database?
Well, say there was a column in your table `selected` which is either 1 (selected) or 0 (not selected) then you could add the selected="selected" HTML attribute based on that value:
	$sql = mysql_query("SELECT `cid`, `first_name`, `surname`, selected FROM `customer`			ORDER BY `surname`			ASC");	$a = 1;	$customer_select .= "<select name=\"cid\" onchange=\"showCustomer(this.value)\">";	$customer_select .= "<option value=\"0\">Customer Name</option>";	while(list($cid, $firstname, $surname, $s) = mysql_fetch_row($sql)) {	if ($s) $selected = "selected=\"selected\""; else $selected = "";	}	$customer_select .= "<option value=\"$cid\" $selected>$firstname $surname</option><br>";	$a++;	}	$customer_select .= "</select>";

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...