Page 1 of 1
newbie: drop down/posting
Posted: Mon Mar 24, 2008 3:51 am
by Dakke
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
Re: newbie: drop down/posting
Posted: Mon Mar 24, 2008 11:32 am
by Christopher
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";
}
Re: newbie: drop down/posting
Posted: Mon Mar 24, 2008 5:14 pm
by Dakke
I have a mysql DB and use this code to get the drop down:
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>");
}
?>
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
Code: Select all
<td>Reeks in het Nederlands: <input type="text" name="series_nederlands" />
and again no clue on what to do.
At least, that's my guess, but...
Re: newbie: drop down/posting
Posted: Mon Mar 24, 2008 6:02 pm
by Dakke
There might be a post that already talks about that kind of stuff:
viewtopic.php?f=1&p=450340
Re: newbie: drop down/posting
Posted: Mon Mar 24, 2008 6:29 pm
by Dakke
So here is where I ended up:
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>
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?
Re: newbie: drop down/posting
Posted: Mon Mar 24, 2008 6:52 pm
by mabwi
You're getting two dropdowns because you have two "select" statements - on line 15 & line 1.
Re: newbie: drop down/posting
Posted: Mon Mar 24, 2008 6:58 pm
by Dakke
Thanks, that indeed did the trick.