newbie: drop down/posting

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

newbie: drop down/posting

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: newbie: drop down/posting

Post 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";
}
 
(#10850)
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

Re: newbie: drop down/posting

Post 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...
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

Re: newbie: drop down/posting

Post by Dakke »

There might be a post that already talks about that kind of stuff:

viewtopic.php?f=1&p=450340
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

Re: newbie: drop down/posting

Post 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?
mabwi
Forum Commoner
Posts: 27
Joined: Wed Aug 01, 2007 4:51 pm

Re: newbie: drop down/posting

Post by mabwi »

You're getting two dropdowns because you have two "select" statements - on line 15 & line 1.
Dakke
Forum Newbie
Posts: 24
Joined: Fri Aug 10, 2007 6:34 pm

Re: newbie: drop down/posting

Post by Dakke »

Thanks, that indeed did the trick.
Post Reply