motti10
motti10

Reputation: 181

How to pass the selected value in dropdown (HTML) to PHP

I am building a select option dynamically. I am selecting from the select option, one value. (values for list come from php How can I pass that selected value to PHP?

<select id ="s1" name="swimopt" class="so">
    <?php echo $options; ?>
</select>' 

THe $options are coming from a MySQL and populating the dropdown

When I select a value, and try

echo $_POST['swimopt'];

does not show selected value

Please help

 <form id="swimdata"  method="POST" action="save.php">

     <input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="boys">BOYS
     <input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender"  value="girls">GIRLS

     <table id="meetTable" style="width:auto">


    <tr>
        <th>EVENT:</th>
        <th>NAME:</th>
        <th>LANE:</th>
        <th>TIME:</th>
        <th>PLACE:</th>
        <th>SCORE 1:</th>
        <th>SCORE 2:</th>
        <th>PLACE:</th>
        <th>TIME:</th>
        <th>LANE:</th>
        <th>NAME:</th>

    </tr>

</table>

     <input type="submit" name="formSubmit" value="Submit" />
     <input type="hidden" name="action1" value="addSwimmer" id="action1">

 </form>

This is my PHP getting the $options from mySQL

if ($result->num_rows > 0) {
    $options= '';
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $options .=  "<tr><td>" . $row["swimmer_id"]. "</td><td>" . $row["first_name"]. " " . $row["last_name"]. " </td><td> "  . $row["school_name"]. "</td></tr>";
    }

} else {
    echo "0 results";
}
echo $options;
$conn->close();

ScreenShots:

Image of Select options in UI

List inside dropdown

Options echoed into select box:

<select id ="s1" name="swimopt" class="so">
    <?php echo $options; ?>
</select>' 

PHP file code

$sql = "select  first_name, last_name  from swimming where gender='m'";

$results = mysqli_query($link,$sql);

if ($results->num_rows > 0) {
    $options = '';
    // output data of each row
    while($row = $results->fetch_assoc()) {
        $fname=$row["first_name"];
        $lname=$row["last_name"];

        $options .= "<option value= >" . $row["first_name"] . "  " . $row["last_name"]."</option>";


    }
} else {
    echo "0 results";
}
echo $options;
$link->close();


?>

Upvotes: 0

Views: 7992

Answers (2)

MakeLoveNotWar
MakeLoveNotWar

Reputation: 954

The select option must be wrapped with html tag <option> with a value attribute, which will be posted if it is selected. According to your given code you are not generating options for a select. So try something like this:

if ($result->num_rows > 0) {
$options= '';
// output data of each row 
while($row = $result->fetch_assoc()) {
    $options .=  "<option value=\"". $row["swimmer_id"] ."\">" . $row["first_name"]. " " . $row["last_name"]. " - "  . $row["school_name"]. "</option>";
}

} else {
echo "0 results";
}
echo $options;
$conn->close();

Now if you $_POST it you will get the swimmer id

Upvotes: 1

Robbie
Robbie

Reputation: 716

I think I see what your problem is. You're not setting a select option. Try this:

<?php
$options = 'John';
?>

<form id="swimdata"  method="POST" action="save.php">

<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="boys">BOYS
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender"  value="girls">GIRLS

<table id="meetTable" style="width:auto">


<tr>
<th>EVENT:</th>
<th>NAME:</th>
<th>LANE:</th>
<th>TIME:</th>
<th>PLACE:</th>
<th>SCORE 1:</th>
<th>SCORE 2:</th>
<th>PLACE:</th>
<th>TIME:</th>
<th>LANE:</th>
<th>NAME:</th>

</tr>

</table>
<select id ="s1" name="swimopt" class="so" value="">
<?php
for($i=0;$i<10;$i++){
echo '<option value="user '.$i.'" >user '.$i.'</option>';
}
?>
</select> 
<input type="submit" name="formSubmit" value="Submit" />
<input type="hidden" name="action1" value="addSwimmer" id="action1">

</form>

Upvotes: 1

Related Questions