...

View Full Version : Displaying data from a database in a drop-down menu



scottyrob
02-13-2006, 03:12 PM
Howdy folks
Using PHP and a database i want to be able to fill in a form.

The database contains the fields:
ID, Attend, Location, Fromto, Leadership

I also have a page with 4 form fields. I would like to be able to have a drop-down box on the page that fills in the form automatticaly depding on the chose you chose from the drop-down. I would like the drop-down to display the data in the Attend field of the database... Any help would be appreciated!

Fet
Edit/Delete Message

degsy
02-13-2006, 04:14 PM
Here is an example



<?php require_once('conn.php'); ?>
<?php
mysql_select_db($database_PHP, $PHP);
$query_seminars = "SELECT id, attend FROM seminar";
$seminars = mysql_query($query_seminars, $PHP) or die(mysql_error());
$row_seminars = mysql_fetch_assoc($seminars);
$totalRows_seminars = mysql_num_rows($seminars);

$colname_chosen_seminar = "-1";
if (isset($_POST['attend'])) {
$colname_chosen_seminar = (get_magic_quotes_gpc()) ? $_POST['attend'] : addslashes($_POST['attend']);
}
mysql_select_db($database_CHF_PHP, $CHF_PHP);
$query_chosen_seminar = sprintf("SELECT * FROM seminar WHERE id = %s", $colname_chosen_seminar);
$chosen_seminar = mysql_query($query_chosen_seminar, $CHF_PHP) or die(mysql_error());
$row_chosen_seminar = mysql_fetch_assoc($chosen_seminar);
$totalRows_chosen_seminar = mysql_num_rows($chosen_seminar);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function chkFrm(el){
if(el.selectedIndex == 0){
alert("Please choose an option");
return false
}
else{
el.form.submit();
}
}
</script>
</head>

<body>
<form name="form1" method="post" action="">
<p>
<select name="attend" onchange="chkFrm(this)">
<option value="Choose" <?php if (!(strcmp("Choose", $_POST['attend']))) {echo "SELECTED";} ?>>Choose Seminar</option>
<?php
do {
?>
<option value="<?php echo $row_seminars['id']?>"<?php if (!(strcmp($row_seminars['id'], $_POST['attend']))) {echo "SELECTED";} ?>><?php echo $row_seminars['attend']?></option>
<?php
} while ($row_seminars = mysql_fetch_assoc($seminars));
$rows = mysql_num_rows($seminars);
if($rows > 0) {
mysql_data_seek($seminars, 0);
$row_seminars = mysql_fetch_assoc($seminars);
}
?>
</select>
</p>
<p>Location
<input name="textfield" type="text" value="<?php echo $row_chosen_seminar['location']; ?>">
<br>
From
<input name="textfield" type="text" value="<?php echo $row_chosen_seminar['from']; ?>">
<br>
Leader
<input name="textfield" type="text" value="<?php echo $row_chosen_seminar['leadership']; ?>">
</p>
</form>
</body>
</html>
<?php
mysql_free_result($seminars);

mysql_free_result($chosen_seminar);
?>

http://www.computer-helpforum.com/php/codingforums/select_populate_form.php

Zegg90
02-13-2006, 04:18 PM
Edit: Never mind, degsy's solved it I think :)

scottyrob
02-13-2006, 05:14 PM
<?php
$db = mysql_pconnect('xx','xx','xx') or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("xx", $db);
$query_seminars = "SELECT ID, Attend FROM Permission_To_Camp";
$seminars = mysql_query($query_seminars, $PHP) or die(mysql_error());
$row_seminars = mysql_fetch_assoc($seminars);
$totalRows_seminars = mysql_num_rows($seminars);

$colname_chosen_seminar = "-1";
if (isset($_POST['Attend'])) {
$colname_chosen_seminar = (get_magic_quotes_gpc()) ? $_POST['Attend'] : addslashes($_POST['Attend']);
}
mysql_select_db($database_CHF_PHP, $CHF_PHP);
$query_chosen_seminar = sprintf("SELECT * FROM Permission_To_Camp WHERE id = %s", $colname_chosen_seminar);
$chosen_seminar = mysql_query($query_chosen_seminar, $CHF_PHP) or die(mysql_error());
$row_chosen_seminar = mysql_fetch_assoc($chosen_seminar);
$totalRows_chosen_seminar = mysql_num_rows($chosen_seminar);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function chkFrm(el){
if(el.selectedIndex == 0){
alert("Please choose an option");
return false
}
else{
el.form.submit();
}
}
</script>
</head>

<body>
<form name="form1" method="post" action="">
<p>
<select name="Attend" onchange="chkFrm(this)">
<option value="Choose" <?php if (!(strcmp("Choose", $_POST['Attend']))) {echo "SELECTED";} ?>>Choose Seminar</option>
<?php
do {
?>
<option value="<?php echo $row_seminars['ID']?>"<?php if (!(strcmp($row_seminars['ID'], $_POST['Attend']))) {echo "SELECTED";} ?>><?php echo $row_seminars['Attend']?></option>
<?php
} while ($row_seminars = mysql_fetch_assoc($seminars));
$rows = mysql_num_rows($seminars);
if($rows > 0) {
mysql_data_seek($seminars, 0);
$row_seminars = mysql_fetch_assoc($seminars);
}
?>
</select>
</p>
<p>Location
<input name="textfield" type="text" value="<?php echo $row_chosen_seminar['Location']; ?>">
<br>
From
<input name="textfield" type="text" value="<?php echo $row_chosen_seminar['Fromto']; ?>">
<br>
Leader
<input name="textfield" type="text" value="<?php echo $row_chosen_seminar['Leadership']; ?>">
</p>
</form>
</body>
</html>

Get an error on line 4... Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/scott/public_html/test.php on line 4

Zegg90
02-13-2006, 06:34 PM
Try removing the $PHP from line four, so it would be:


$seminars = mysql_query($query_seminars) or die(mysql_error());

scottyrob
02-13-2006, 06:52 PM
Hum..

$query_seminars = "SELECT ID, Attend FROM 'Permission_To_Camp' ";

gets

You have an error in your SQL syntax near ''Permission_To_Camp' ' at line 1

Previous error fixed though!

degsy
02-14-2006, 03:18 PM
It should be fine. You may have an error before that in your connection code.

Post the code you are using now.

rhindo
04-18-2006, 04:05 AM
I continue to get errors in syntax on line 1. I can not seem to get the conn.php file correct.

Could you post a sample?

degsy
04-18-2006, 05:10 PM
<?php
$hostname_CHF_PHP = "host";
$database_CHF_PHP = "dn_name";
$username_CHF_PHP = "user_name";
$password_CHF_PHP = "pass";
$CHF_PHP = mysql_pconnect($hostname_CHF_PHP, $username_CHF_PHP, $password_CHF_PHP) or trigger_error(mysql_error(),E_USER_ERROR);
?>

rhindo
04-19-2006, 01:15 AM
Two questions that are proabably related.

If I add any other fields to this form, they are zeroed out when I use this

also

I need to have several groups of these text boxes on the form. How can I dissassociate them so that when I change one of the drop down boxes, it only updates the corresponding text and not every group

I posted my code below

rhindo
04-19-2006, 01:26 AM
<?php require_once('conn.php'); ?>

<?php
mysql_select_db($database_PHP, $PHP);
$query_labors = "SELECT id, code FROM labor";
$labors = mysql_query($query_labors, $PHP) or die(mysql_error());
$row_labors = mysql_fetch_assoc($labors);
$totalRows_labors = mysql_num_rows($labors);

$colname_chosen_labor = "-1";
if (isset($_POST['code'])) {
$colname_chosen_labor = (get_magic_quotes_gpc()) ? $_POST['code'] : addslashes($_POST['code']);
}
mysql_select_db($database_CHF_PHP, $CHF_PHP);
$query_chosen_labor = sprintf("SELECT * FROM labor WHERE id = %s", $colname_chosen_labor);
$chosen_labor = mysql_query($query_chosen_labor, $PHP) or die(mysql_error());
$row_chosen_labor = mysql_fetch_assoc($chosen_labor);
$totalRows_chosen_labor = mysql_num_rows($chosen_labor);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function chkFrm(el){
if(el.selectedIndex == 0){
alert("Please choose an option");
return false
}
else{
el.form.submit();
}
}
</script>
</head>

<body>
<form name="form1" method="post" action="">
<p>
<select name="code" onchange="chkFrm(this)">
<option value="Choose" <?php if (!(strcmp("Choose", $_POST['code']))) {echo "SELECTED";} ?>>Choose labor</option>
<?php
do {
?>
<option value="<?php echo $row_labors['id']?>"<?php if (!(strcmp($row_labors['id'], $_POST['code']))) {echo "SELECTED";} ?>><?php echo $row_labors['code']?></option>
<?php
} while ($row_labors = mysql_fetch_assoc($labors));
$rows = mysql_num_rows($labors);
if($rows > 0) {
mysql_data_seek($labors, 0);
$row_labors = mysql_fetch_assoc($labors);
}
?>
</select>

Description
<input name="text1" type="text" value="<?php echo $row_chosen_labor['desc']; ?>">
Amount
<input name="text2" type="text" value="<?php echo $row_chosen_labor['amount']; ?>">
Quantity
<input name="txtQty" type="text" value="">
</p>

<p>
<select name="code" onchange="chkFrm(this)">
<option value="Choose" <?php if (!(strcmp("Choose", $_POST['code']))) {echo "SELECTED";} ?>>Choose labor</option>
<?php
do {
?>
<option value="<?php echo $row_labors['id']?>"<?php if (!(strcmp($row_labors['id'], $_POST['code']))) {echo "SELECTED";} ?>><?php echo $row_labors['code']?></option>
<?php
} while ($row_labors = mysql_fetch_assoc($labors));
$rows = mysql_num_rows($labors);
if($rows > 0) {
mysql_data_seek($labors, 0);
$row_labors = mysql_fetch_assoc($labors);
}
?>
</select>

Description
<input name="text1" type="text" value="<?php echo $row_chosen_labor['desc']; ?>">
Amount
<input name="text2" type="text" value="<?php echo $row_chosen_labor['amount']; ?>">
Quantity
<input name="txtQty" type="text" value="">
</p>
</form>
</body>
</html>
<?php
mysql_free_result($labors);

mysql_free_result($chosen_labor);
?>here is my code if that helps...

degsy
04-19-2006, 04:18 PM
The script is not setup to do that. For that you would need a seperate script for each box.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum