Hi,
I've been looking for it, but as always a noob gets lost in the information. So a simple start:
I have a form that uses post to get a picture stored in a dir, to fill a table with some values and works great. But there is one column with a more dynamic need.
I got a table that is filled with some values. I would like to use those values as a drop down list in the form. Once a value is choosen, that value should be posted in the second table. So drop down of first table, post value in second.
Where to start?
I suppose a drop down using php,
post using...?
D
newbie: drop down/posting
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: newbie: drop down/posting
What kind of database do you have? The basic logic is:
Code: Select all
$db = new DBConnection();
$result = $db->query("SELECT * FROM mytable");
if (! $db->error()) {
$output = "<select name="foo">";
while ($row = $result->fetch()) {
$output .= "<option value="{$row['value']}">";
}
$output .= "</select>";
} else {
$output = "Error";
}
(#10850)
Re: newbie: drop down/posting
I have a mysql DB and use this code to get the drop down:
I works just fine, but know I need to get the selected value posted in the db. So in fact I use the table 'category' to populate the dropdown, and want the selected value to be posted into another table (more specific into the column 'category' of the second table). I thought of it using onChange, but have no clue on where to start. This code must become a form, and when the value changes that it is posted in a hidden field perhaps, and that that hidden field will be posted in the table?
Or even more simple than that: integrate the php script in a form (no idea if that's possible) and than simply post the value of field name 'category' into the table. That would be somewhere in the and again no clue on what to do.
At least, that's my guess, but...
Code: Select all
<?php
// Open DB
include("php/library/config.php");
include("php/library/opendb.php");
// Populate Drop Down
$query = "select categorie_nederlands From category";
$results = mysql_query($query, $conn) or die("Error performing query");
if(mysql_num_rows($results) > 0){
echo("<select name=\"selectItem\">");
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->ID\">$row->categorie_nederlands</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>
Or even more simple than that: integrate the php script in a form (no idea if that's possible) and than simply post the value of field name 'category' into the table. That would be somewhere in the
Code: Select all
<td>Reeks in het Nederlands: <input type="text" name="series_nederlands" />At least, that's my guess, but...
Re: newbie: drop down/posting
So here is where I ended up:
Its part of a bigger form, so the <form> tags are there, not posted in the code above.
This does not return the drop down box. In fact it returns 2 drop downs. One populated with nothing, another populated with the php. So the select creates a select but doesn't use the data of the php code.
What is wrong with the above code?
Code: Select all
<select name="category">
<?php
// Open DB
include("php/library/config.php");
include("php/library/opendb.php");
// Populate Drop Down
$query = "select categorie_nederlands From category";
$results = mysql_query($query, $conn) or die("Error performing query");
if(mysql_num_rows($results) > 0){
echo("<select name=\"selectItem\">");
while($row = mysql_fetch_object($results)){
echo("<option value=\"$row->ID\">$row->categorie_nederlands</option>");
}
echo("</select>");
}
else{
echo("<i>No values found</i>");
}
?>
</select>
This does not return the drop down box. In fact it returns 2 drop downs. One populated with nothing, another populated with the php. So the select creates a select but doesn't use the data of the php code.
What is wrong with the above code?
Re: newbie: drop down/posting
You're getting two dropdowns because you have two "select" statements - on line 15 & line 1.
Re: newbie: drop down/posting
Thanks, that indeed did the trick.