Jump to content

Dynamic Drop Down Box


nealios

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...