Forum Moderators: coopster

Message Too Old, No Replies

Passing a value to another page from a drop down box

how do i do this?

         

danielm28

1:12 am on Jan 31, 2005 (gmt 0)

10+ Year Member



I have a form that has an autopopulated drop down list from a table. The value of each option is the record ID and the text that you see in the drop down is a field coresponding to that record.
How can I pass the field name corresponding to the record ID to the next page? I need to be able to print this name on the next page.

I understand that I need to have a hidden field in the form to pass the info, but what would the value of this input be? I know how to pass the record Id, but not a coresponding record name.

here is my form code:

<form method="post" action="tracking_results.php">
Begin Date: <input type="text" name="begin_date"><br>
End Date: <input type="text" name="end_date"><br>
Dealer: <select name="dealer_list">
<option selected value="">All</option>

<?
$dbh = mysql_connect($db_server, $db_user, $db_pass)
or die("could not connect to database");

mysql_select_db($db_name) or die("failed to select database");

$sql = "SELECT name, city, state,zip, dealer_id FROM Dealer ORDER BY name, city, state,zip";
$sql = mysql_query($sql);
while($row = mysql_fetch_assoc($sql)) {
$row['name'] = htmlentities($row['name']);
echo '<option value='.$row[dealer_id].'>'.$row[name].' - '.$row[city].','.$row[state].' '.$row[zip].'</option>';
}

?></select><br><br>
<input type="submit" value="submit" name="submit">
</form>

Thanks

Zipper

1:34 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



you can obviously use the dealer_id to retreive the field name from the database. but if you do not have server-side previledges in the destination page you can go for a client side method using javascript.

first create the hidden input field


<input type="hidden" name="dealer_data" value="All">

then set the function to change the details
<select name="dealer_list" onChange="doChange();">

and here's the function.. put it within the head tags


<script language="javascript">
function doChange(){
dealer_id = document.forms[0].dealer_list.selectedIndex;
document.forms[0].dealer_data.value = document.forms[0].dealer_list.options[dealer_id].text;
}
</script>

if you have a another form before this one, name the form, replace forms[0] with the new value.

danielm28

2:56 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



zipper,

i do have server side priveleges in the destination page. do i need to write an additional query in the destination page via the dealer_id to get the associated name? I was hoping that since dealer name is available in the returned recordset on the initial page I could somehow pass it to the destination page via a hidden field. is this possible? if so, what code do i place in the hidden field to make this happen?

thanks

Zipper

4:52 pm on Jan 31, 2005 (gmt 0)

10+ Year Member



let me see, you are displaying four fields (name, city, state, zip) in each option in the drop-down menu. So if you use the client side method as i pointed out previously, you will get a combination of these strings in the destination page. so if you only need the dealer name, then your option is to use the server-side method.

anyway, let me point out both ways so you can understand it well.

client-side approach


<html>
<head>
<script language="javascript">
function doChange(){
dealer_id = document.dealers.dealer_list.selectedIndex;
document.dealers.dealer_data.value = document.dealers.dealer_list.options[dealer_id].text;
}
</script>
</head>
<body>
<form name="dealers" method="post" action="tracking_results.php">
<input type="hidden" name="dealer_data" value="">
Begin Date: <input type="text" name="begin_date"><br>
End Date: <input type="text" name="end_date"><br>
Dealer: <select name="dealer_list" onChange="doChange();">
<option selected value="">All</option>
<?
$dbh = mysql_connect($db_server, $db_user, $db_pass)
or die("could not connect to database");

mysql_select_db($db_name) or die("failed to select database");

$sql = "SELECT name, city, state,zip, dealer_id FROM Dealer ORDER BY name, city, state,zip";
$sql = mysql_query($sql);
while($row = mysql_fetch_assoc($sql)) {
$row['name'] = htmlentities($row['name']);
echo '<option value='.$row[dealer_id].'>'.$row[name].' - '.$row[city].','.$row[state].' '.$row[zip].'</option>';
}
?>
</select><br><br>
<input type="submit" value="submit" name="submit">
</form>
</body></html>

and you can view them in the tracking_results.php (destination)


<?
echo "Dealer ID: $_POST['dealer_list'] \n";
echo "Dealer Details: $_POST['dealer_data']";
?>

Here you will see all four fields under Dealer Details.

server-side approach

Just use your initial form as it is and query the db to get the dealer name in the destination page.


<?
$dealer_id = $_POST['dealer_list'];

mysql_connect($db_server, $db_user, $db_pass)
or die("could not connect to database");
mysql_select_db($db_name) or die("failed to select database");

$sql = "SELECT name FROM Dealer WHERE dealer_id = $dealer_id";

$dealer_name = mysql_result(mysql_query($sql),0) or die(mysql_error());

echo "Dealer ID: $dealer_id \n";
echo "Dealer Name: $dealer_name";
?>

ergophobe

5:56 pm on Jan 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



[edit: Zipper beat me to it, but here's a shorter take on the specific question of how to get the name from one page to the next using a hidden field that just passes the value without executing another query or using javascript. If you have a huge number of dealers in the drop down, this will bring the page size up a bit]

To pass this as a hidden field, you would do something like this (note also that you are missing many required quotes in your first version - I've bolded them in this one):

while($row = mysql_fetch_assoc($sql)) {
$row['name'] = htmlentities($row['name']);
echo '<option value="'.$row['dealer_id'].'">'.$row['name'].' - '.$row['city'].','.$row['state'].' '.$row['zip'].'</option>';

echo '<input type="hidden" name="dealer_name[' . $row['dealer_id'] . ']" value="'.$row['name'].'">';
}

In your destination page, you'll have
$dealer_id = $_POST['dealer_list'];
$dealer_name = $_POST['dealer_name'][$dealer_id];

danielm28

11:24 pm on Feb 1, 2005 (gmt 0)

10+ Year Member



thank u very much zipper and ergophobe. you have given me the answer that i was looking for.