I am trying to design an input form based on a drop down list which uses an sql query as its source.
The following Query staement produces a drop down list but so far I have been unable to incorporate POST and Submit statements to allow me to pass the selected variable .
Code so far :-
<?
$username="uname";
$password="pwd";
$database="my_db";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query('SELECT * FROM `daysofweek`') or die(mysql_error());
echo '<select name="Day">';
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['1'].'">'.$row['Day'].'</a>';
}
echo '</select>';
?>
Can anyone suggest the appropriate structure to create the form.
Thanks
Dave
Form with drop down list from query
Moderator: General Moderators
-
dave.hawksworth
- Forum Newbie
- Posts: 11
- Joined: Thu Feb 18, 2010 8:45 am
Re: Form with drop down list from query
You need to put your select in a form for it to be posted to another page. (I have made a tweak to your option as well as it was the incorrect closing tag)
You will need to change destination.php to the php file the page that you want to post this data to. You can then access the posted data as below
If you are having trouble accessing the post data it is sometimes helpful to print everything that has been posted to the screen
Code: Select all
echo '<form method="post" action="destination.php">';
echo '<select name="Day">';
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="'.$row['1'].'">'.$row['Day'].'</option>';
}
echo '</select>';
echo '<p><input type="submit" value="Query">';
echo '</form>';Code: Select all
echo $_POST['Day'];Code: Select all
print_r($_POST);-
dave.hawksworth
- Forum Newbie
- Posts: 11
- Joined: Thu Feb 18, 2010 8:45 am
Re: Form with drop down list from query
Thanks for the quick response.
I have used your code and everthing appears to work smoothly except that the posted string is a blank.
( I confirmed this with a print command in the target script as you recomended.
Can you see why the posted variable is blank.
Thanks
Dave
The code now reads:-
<?
$username="user";
$password="pwd";
$database="my_db";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query('SELECT * FROM `daysofweek`') or die(mysql_error());
echo '<form method="post" action="array_script.php">';
echo '<select name="Day">';
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="'.$row['1'].'">'.$row['Day'].'</option>';
}
echo '</select>';
echo '<p><input type="submit" value="submit">';
echo '</form>';
?>
I have used your code and everthing appears to work smoothly except that the posted string is a blank.
( I confirmed this with a print command in the target script as you recomended.
Can you see why the posted variable is blank.
Thanks
Dave
The code now reads:-
<?
$username="user";
$password="pwd";
$database="my_db";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query('SELECT * FROM `daysofweek`') or die(mysql_error());
echo '<form method="post" action="array_script.php">';
echo '<select name="Day">';
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="'.$row['1'].'">'.$row['Day'].'</option>';
}
echo '</select>';
echo '<p><input type="submit" value="submit">';
echo '</form>';
?>