ok i have a AJAX drop down list for Country that when slected looks up data from the database and fills the next drop down list with the right data.

The problem im having is i cant get the value from the second drop down list to post with the rest of the form??? Can anyone help or give ideas on when its not working.

here is the code.

HTML

<html>
<head>
<script type="text/javascript" src="selectCountry.js"></script>
</head>
<body>

<form action="main2.php" method ="POST">
Country:
<select onchange="showCountry(this.value)" name="country" >
<option value="">Select one</option>
<option value="1">US</option>
<option value="2">UK</option>
<option value ="3">France</option>
</select>

<input type='submit' value='submit'>
</form>

<p id="here">City: <input type='text' name="city"></p>

</form>

</body>
</html> 

JAVA SCRIPT

var xmlhttp;

function showCountry(str)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Your browser does not support HTTP Request!");
		return;
	}
	var url="getCountry.php";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function stateChanged()
{
	if (xmlhttp.readyState==4)
	{
		document.getElementById("here").innerHTML=xmlhttp.responseText;
	}
	else 
	{
		document.getElementById("here").innerHTML = '<img src="ajax-loader.gif"> Loading..';
	}
}


function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

PHP

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);

if ($q == 1)
{
	$sql= mysql_query("SELECT * FROM states");

echo "State: ";
echo "<select name='state'>";
		echo "<option value=''>Select one</option>";
while($row = mysql_fetch_array($sql))
	{
		echo "<option value ='1'>".$row['state']."</option>";
	}	
	echo "</select>";


echo "<p>City: <input type='text'></p>";


}
else if ($q == 2)
{
	$sql= mysql_query("SELECT * FROM ukTowns");

echo "City: ";
echo "<select name='city'>";
		echo "<option value=''>Select one</option>";
while($row = mysql_fetch_array($sql))
	{
		echo "<option value ='1'>".$row['town']."</option>";
	}	
	echo "</select>";

}
else if ($q != 1 || $q !=2)
{
	echo "<p>City: <input type='text'></p>";
}



mysql_close($con);
?> 

thanks for looking ....:quiet:

    in the second dropdowns you did not passed the correct ID's, all records embedded as 1 ...

             echo "<option value ='1'>".$row['state']."</option>"; 
      djjjozsi;10918163 wrote:

      in the second dropdowns you did not passed the correct ID's, all records embedded as 1 ...

               echo "<option value ='1'>".$row['state']."</option>"; 

      i dont really understand what your saying. its in a loop and fills the drop down with all the states i have in my database table

      i will prob have something like this.

      echo "<option value ='".$row['state']."'>".$row['state']."</option>"; 

      but this is not my problem i cant get the values from states or city to POST to the next page (main2.php)

      only the country value is posted ??

        check your .php file in your browser, becouse its working for me.

        getCountry.php?q=1
        getCountry.php?q=2
        getCountry.php?q=3

          hmm thats wired which php file do you think i should be looking at, main2.php?

          echo $_POST['country'];
          echo $_POST['state'];
           echo $_POST['city'];
          

            in the AJAX code, i see a file:

            getCountry.php

            if you type in the browser:
            getCountry.php?q=1

            you can test the output code, which comes back from the .php file. (most of the cases the source code view can show the real output... )

            if you have a FORM back, you can submit that form, if its using POST method, you get the values.

            if your're making complicated codes, you should pass the div's ID name to the stateChanged() function, and the innerHTML values coould updated.

              Write a Reply...