I want to get the choice of my drop down menu variable '$Countryname'

I have this script which it create a drop down menu from Country field from mysql database, if someone kindly can help me to get the choice of my country drop down menu variable ‘$Countryname’’ which I want to use it in another select to get more data from the table:

$query=(“SELECT * FROM rate”);
$result=mysql_query($query) or die (“Unable to Make the Query:” . mysql_error() );
echo “<select name=myselect>”;
while($row=mysql_fetch_array($result)){
echo “<OPTION VALUE=”.$row[‘Country_name’].“>”.$row[‘Country_name’].“</OPTION>”;
}
echo “</select>”;
mysql_close($connection);
?>

this is the second select i want to use:

$query = “SELECT Country_name,2_dollar_card,5_dollar_card,10_dollar_card FROM rate WHERE Country_name Like ‘$Countryname’”;

Thank you

echo “<select name=myselect>”;

Well this needs fixing.

echo “<select name=‘myselect’>”;

there we go.

Now, when the form is submitted, the value given will either be in $_POST[‘myselect’] or $_GET[‘myselect’] depending on which method you used on your form tag (get or post)

I am guessing you want to reuse the country name you got from your first select:


$query = "SELECT Country_name ... etc 
WHERE Country_name = '" . $row['Country_name']. "'";
// watch out for the quotes here

@starlion <select name=myselect> is perfectly valid if it contains no spaces, as I understand it. Some strict w3c validators might squeal depending on your DTD defn, but it should work fine. Is there a browser issue with unquoted names?

sorry i was a way i just got back
here is the code it still not working i tried both solution still can’t get the myselect
<?php
// Connect
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die(“Could not connect to the database: <br />”. mysql_error());
}
// Select the database
$db_select = mysql_select_db($db_database);
if (!$db_select){
die (“Could not select the database: <br />”. mysql_error());
}

$query=(“SELECT * FROM rate”);
$result=mysql_query($query) or die (“Unable to Make the Query:” . mysql_error() );
//echo "<select name=‘myselect’ onchange=“echo(‘$mySelect’)”;
echo “<select name=‘MySelect’>”;

while($row=mysql_fetch_array($result)){
echo “<OPTION VALUE=”.$row[‘MySelect’].“>”.$row[‘Country_name’].“</OPTION>”;
echo “<tr><td>Job:</td><td>”.$_POST[‘$myselect’].“</td></tr>”;
echo “<tr><td>Country:</td><td>”.$_POST[‘MySelect’].“</td></tr>”;

}
$i =$myselect;
while ($i < 1008){
$i ++;
$myselect= “Option”.$i;
echo “<option value=\”$i\">$myselect</option>
";

}

echo "</select>
";
echo “</select>”;

mysql_close($connection);
?>

Wrap your code in tags like this [ php ] [ /php ] (without the spaces) so we can read it.

Make your sql statement more explicit don’t use "select * from rate " but “select id, Country name from rate” (or what are the names of the fields in your table?)

Pass this query to your database and show us the output;


DESCRIBE rate


<form name="form1" action="d3.php" method="POST">
 
<?php /*?><tr><td align="right">Country:</td><td><input type="text" name="myselect"/></td></tr><?php */?>

</form>
</table>
<?php
// Connect
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die("Could not connect to the database: <br />". mysql_error());
}
// Select the database
$db_select = mysql_select_db($db_database);
if (!$db_select){
die ("Could not select the database: <br />". mysql_error());
}

$query=("SELECT countryid,Country_code,Country_name,2_dollar_card  FROM rate");
$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() );
//echo "<select name='myselect' onchange="echo('$mySelect')";
echo "<select name='MySelect'>";

while($row=mysql_fetch_array($result)){ 
echo "<OPTION VALUE=".$row['MySelect'].">".$row['Country_name']."</OPTION>";
echo "<tr><td>Job:</td><td>".$_POST['$myselect']."</td></tr>";
echo "<tr><td>Country:</td><td>".$_POST['MySelect']."</td></tr>";

}
$i =$myselect;
while ($i < 1008){
   $i ++;
   $myselect= "Option".$i;
   echo "<option value=\\"$i\\">$myselect</option>\
";
   
}

echo "</select>\
";
echo "</select>";

mysql_close($connection);
?>

<?php echo "<tr><td>Country:</td><td>".$_POST['myselect']."</td></tr>"; ?>
<tr><td><p><input type="submit" /></p></td></tr>
	</form>	

Ok, lets imagine the world has only a few countries for now and the first country is “Afghanistan” its Country_code is “AF” its 2_dollar_code is “2” and its countryid is “1”.

Then you want to build a form such as this;

form.php


<form method="POST" action="postback.php">
<select name=countryid>
<option value='1'>Afghanistan</option>
<option value='2'>Albania</option>
<option value='3'>Aston Villa</option>
</select>
<input type="submit" />
</form>

postback.php


<?php
var_dump($_POST);

$countryid = (int)$_POST['countryid'] ;

$query = "SELECT Country_name,2_dollar_card,5_dollar_card,10_dollar_card 
FROM rate WHERE countryid = " . $countryid;

echo "<hr  /> 
Post that into your database and make sure your SQL bit is working properly
</hr />";

So, do you know how to build a list of countries, or is that the main question?

Hi this is very obvious drop down menu you add the option values to it, the main problem I am having is I want the drop down to read from the database column country_name then when I select the Country from the drop down menu this selection I need to use it to get some other data.
Thank you

So does the other data have to appear on that same page without it completely refreshing?

the other data could appear in the result page or any where, the issue is to get that selected country from the drop down menu


SELECT Country_name,2_dollar_card,5_dollar_card,10_dollar_card 
FROM rate WHERE countryid = 1;

So does that query get the data you need if you wanted Afghanistan?

You seem to be putting invalid code in the select the format of the select input should be:

<select>
<option value='1'>
<option value='2'>
<option value='3'>
</select>

You are putting <td> within the select so that will cause a rendering error in the browser.

for the code to create the select drop down box you should try something along the lines of:


echo "<select name='MySelect'>";
while($row=mysql_fetch_array($result)){ 
echo "<OPTION VALUE=".$row['country_id'].">".$row['Country_name']."
</OPTION>";
}
echo "</select>";

You shouldn’t put anything more in there.

Alex

I’m lost as to why there is 11 responses but not one answer for neosaid, his question was very simple yet everyone is avoiding it. Basically what you want is for when you select a drop down item the value to make another MySQL request to then show extra data on the page. Now there are 2 ways to go about this…

  1. Use AJAX via javascript and request the extra data through the same file or a secondary file that runs the request
  2. Use an onchange event in the select box to redirect the user to the same page but with the country id appended to the URL.

Personally the easiest way is to just use the onchange event and some quick PHP updates but a much nicer way is the AJAX request which is a lot smoother. Below i have posted the code for the onchange redirect…

echo "<select name='MySelect' onchange='redirect(" . $_SERVER['PHP_SELF'] . ", this.value)'>";
function redirect(self, app) {
    window.location = self + '?country_id=' + app;
}

@alexson: No offense to you or your coding technique but closing </option> is 100% valid markup, if it was incorrect all my HTML would be invalid but passes through XHTML 1.0.

sorry wasn’t very clear there, I didn’t mean about closing the <option> tags. I was referring to:

while($row=mysql_fetch_array($result)){ 
echo "<OPTION VALUE=".$row['MySelect'].">".$row['Country_name']."</OPTION>";
echo "<tr><td>Job:</td><td>".$_POST['$myselect']."</td></tr>";
echo "<tr><td>Country:</td><td>".$_POST['MySelect']."</td></tr>";

which looked like it was echoing out <tr> and <td> elements within the <select> tag which wasn’t closed until further down.

Off Topic:

@alexson: Sorry thought you meant something else, please ignore my comment :slight_smile: