tags:

views:

46

answers:

2

Hi,

I am using this code in my form to create a drop down menu. (the list of options loads corrects from my sql database). Once the user hits submit, I should be able to retrieve the value selected with $_POST['field'].

<form action="page2.php" method="post" name="form" id="form">

 <?php 
$query = sprintf("SELECT domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo "<select name=domaine value=''>Domain </option>";

while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[id]>$nt[domaine]</option>";
}
echo "</select>";
?>
...

On the second page, I use this code:

$domaine = strip_tags(substr($_POST['domaine'],0,32));
echo "You selected $domaine";

But I get nothing a blank value, what am I doing wrong?

Thanks!

+1  A: 

This line is probably incorrect...

echo "<select name=domaine value=''>Domain </option>";

Should it be

echo "<select name=domaine value=''>";

You should also note that if none of the options are selected, then you won't get a value back. To ensure you get a value back, select one of them (eg the first one) by default, by adding selected="selected" to it...

I'd also recommend quoting values a little more clearly. For the sake of completeness...

<?php 
$query = sprintf("SELECT domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);

echo '<select name="domaine" value="">';

$isfirst = true;
while ($nt=mysql_fetch_array($result)) {
    echo '<option value="'.$nt[id].'"';
    if ($isfirst) 
        echo ' selected="selected"';
    echo '>'.$nt[domaine].'</option>';
    $isfirst = false;
}

echo '</select>';
?>
rikh
I subtituted with the code you suggested Rikh, but I am still getting the same blank output at the end. This is the HTML code I get: <select name=domaine value=''><option value=>test.net</option><option value=>test2.com</option></select>
Samantha
You are wrong. Even if there are no "selected=selected" it will still have a value if you have any options in the HTML.
methodin
@rikh: By default the first option is selected unless you specify selected="selected" for a specific option.
Parkyprg
@methodin fair enough. I always specify a default for clarity, I guess it made me think it was required.
rikh
+1  A: 

In your query you didn't selected the id, only the domaine. Change it to be like this:

<form action="page2.php" method="post" name="form" id="form">

 <?php 
$query = sprintf("SELECT id, domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo '<select name="domaine">';

while($nt=mysql_fetch_array($result)){
echo '<option value="$nt[id]">$nt[domaine]</option>';
}
echo "</select>";
?>
Parkyprg
Thanks Parkyprg, now the second page gets the selected value, but the values in the first page aren't retrieved correctly from the database anymore! This is the HTML I get in the first page: <select name="domaine"><option value="$nt[id]">$nt[domaine]</option><option value="$nt[id]">$nt[domaine]</option></select>
Samantha
Change to: echo '<option value="'.$nt[id].'">'.$nt[domaine].'</option>';
Parkyprg