Jump to content

HOW TO SHOW OTHER DATA WHEN A COUNTRY NAME IS SELECTED FROM A LIST BOX


rumon007

Recommended Posts

I have a list box named select_country showing a list of countries.

All the country names come from a mysql database table named call_rate.

I also created a text field and a button in my form.

I have written the following code to show all country names in the list box:

 

CODE:

 

$conn = mysql_connect("localhost", "Administrator", 7216708)

            or die(mysql_error());

  mysql_select_db("test",$conn)

  or die(mysql_error());

$get_country="select id,country_name,rate from call_rate";

$get_country_result= mysql_query($get_country) or die(mysql_error());

 

<table width=50% hight=90% cellpadding=10 cellspacing=0 border=1 align=center>

<tr>

<td>

<select name=\"select_country\">";

while($country_result= mysql_fetch_array($get_country_result))

{

    $country_id=$country_result[id];

    $country_name=$country_result[country_name];

    $call_rate=$country_result[rate];

    $display_block.="<option value=\"$country_id\">$country_name</option>";

}

   

$display_block.= "</select></td>";

$display_block.= "

<td><input type = \"text\" name=\"edit_rate\" size=10></td>

<td><input type = \"submit\" name = \"submit\" value=\"Update\"></td>

<input type = \"hidden\" name = \"op\" value = \"edit\">

</tr>

</table>";

 

And finally I printed the $display_block string.

 

What I am trying to do is whenever I will select a country name from the list the associated call rate will be displayed in the text field. (‘rate’- field name in database).

And when after any change in rate when I will click the button the new rate will replace the old rate in the database.

 

Please help me in writing the code for doing this!!!

 

 

with ajax this can be done add this script to your head of your page

 

<script language="javaScript" type="text/javascript">
function getXMLHTTP() { //fuction to return the xml http object
	var xmlhttp=false;	
	try{
		xmlhttp=new XMLHttpRequest();
	}
	catch(e)	{		
		try{			
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e){
			try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e1){
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
    }

function getState(codeId)
{
   var strURL="statecall.php?countrycode="+codeId;
   var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()

     {
      if (req.readyState == 4)
      {
 // only if "OK"
 if (req.status == 200)
         {
    document.getElementById('statediv').innerHTML=req.responseText;
 } else {
   	   alert("There was a problem while using XMLHTTP:\n" + req.statusText);
 }
       }
      }
   req.open("GET", strURL, true);
   req.send(null);
   }
}
</script>

 

add a div tag named 'statediv' eg.. (<div name='statediv'></div>) on your page where you want the data to appear

 

change your select tag in your form to this

<select name="select_country" onchange="getState(this.value);">

 

create a page called statecall.php, this is your new content, that will replace the div tag, so do your queries here in this example it creates the option tags for a state select box, sending a variable called $countrycode, the variable $countrycode will be the value of the country select box selected in the main page.

 

code example

<select name="state" id="state">
<option value="--Please Select State/Province--">--Please Select State/Province--</option>
<?php 
// include database connection 
include('data_connect.php');
// print state select box
		$query = "SELECT * FROM states WHERE code='$countrycode' ORDER BY nameorder";
		$result = mysql_query($query);
		while($row = mysql_fetch_array($result)) {
		$spname = $row["name"];
		$spcode = $row["code"];
		echo "<option value='$spname'>$spname</option>\n";
		}
?>
</select>

 

this should solve your problem :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.