Forum Moderators: coopster
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
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.
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
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']";
?>
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";
?>
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];