codenamejupiterx
codenamejupiterx

Reputation: 1619

PHP trying to echo value of select dropdown to screen

I have this simple block of code that that prints a table to the screen. This table has three columns and id ,car(drop down list with three choices ford, toyota, and gmc) and name. What i'm trying to do is to print the value of a particular rows drop down list selection to the screen. My method is not working....

<?php    
//////connecting to our sql server
$db_connect=mysql_connect("XXXXXXXXXXXX", "XXXXXXXXX", "XXXXXXXXXXXXX")    
 or die("not logged in");
//////now selecting our database
$db_select=mysql_select_db("XXXXXXXXXXX") or die(mysql_error());
////query
$query = mysql_query("SELECT * FROM car "); 


echo '<form action="drop_down_car_test.php?" method="GET">';
echo '<table border = \"1\">';
echo '<tr><td>id</td><td>car</td><td>name</td>';

while($row=mysql_fetch_array($query)){
echo "<tr><td>";

////in the database 'id' is the primary auto incremented id
echo $row['id'];
echo "</td><td>";

echo "<select name='carDropDown".$row['id']."' >;
<option value=\"1\">ford</option>;
<option value=\"2\">toyota</option>;
<option value=\"3\">gmc</option>;
</select>";

echo $_GET['carDropDown'.$row['id']];

echo "</td><td>";
echo $row['name'];
echo "</td><td>";
}////end while
echo"<table>";

echo '<td bgcolor=#7FFF00><input type="submit" value="Update"></td>';
echo "</form>";
?>

Upvotes: 0

Views: 15659

Answers (2)

Kai Qing
Kai Qing

Reputation: 18843

This is wrong:

echo "<select name='carDropDown' id='carDropDown'".$row['id']." >;
<option value=\"1\">ford</option>;
<option value=\"2\">toyota</option>;
<option value=\"3\">gmc</option>;
</select>";

echo $_GET['carDropDown'.$row['id']];

The value you want to echo with THIS html is just:

$_GET['carDropDown']

Remember, it is the "name" parameter that assigns the value to the superglobals. id is not used in these vars.

But since you use this in a loop, I bet you want something like this:

echo "<select name='carDropDown".$row['id']."'>;
<option value=\"1\">ford</option>;
<option value=\"2\">toyota</option>;
<option value=\"3\">gmc</option>;
</select>";

echo $_GET['carDropDown'.$row['id']];

But here's yet another problem... If you intend to use this somehow in processing, you will need to know all of these id's to fetch your values. Why not use an array then?

<select name='carDropDown[]'>

Then you can loop through those like so:

<?php foreach($_GET['carDropDown'] AS $car): ?>

    <?php echo $car; ?><br>

<?php endforeach; ?>

Either way, something looks fishy here. Are you intending to have every row in the car table have a select box with ford, toyota or gmc as options? Or are you trying to have a select box with all car makes in it that - when changed - populates your query and results with cars of that make?

Upvotes: 1

rashdanml
rashdanml

Reputation: 31

You don't appear to be checking whether the submit button has been clicked. Something like:

if (isset($_GET['submit'])) {
echo value;
}

Upvotes: 0

Related Questions