Hi guys I need some help. Ive followed this online tutorial to the letter to get to learn so Ajax everything works fine till i try my third dropdown it gives the error Notice : Undefined index: state in C:\xampp\htdocs\loginandout\testandlearnajax2.php on line 5
I know what it means but why does Ajax loose the value. as i said in the Tutorial it works fine unless im missing something.
<?php
include('dbconnection.php');
?>
<form name=form1 action="" method="post">
<table>
<tr>
<td>select country</td>
<td><select id ="countrydd" onChange="change_country()">
<option>Select</option>
<?php
$sql = "select * from countries";
$result = mysqli_query($link11,$sql);
while($row=mysqli_fetch_array($result)){
?>
<option value = <?php echo $row['id'];?>><?php echo $row['name'];?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td>Select State</td>
<td>
<div id="state">
<select>
<option>Select</option>
</select>
</div>
</td>
</tr>
<tr>
<td>Select city</td>
<td>
<div id="city">
<select>
<option>Select</option>
</select>
</div>
</td>
</tr>
</table>
<form>
<script type="text/javascript">
//Function onchange to populate second dropdown
function change_country()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","testandlearnajax2.php?country="+document.getElementById("countrydd").value,false);
xmlhttp.send(null);
document.getElementById("state").innerHTML=xmlhttp.responseText;
}
function change_state()
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","testandlearnajax2.php?state="+document.getElementById("statedd").value,false);
xmlhttp.send(null);
document.getElementById("city").innerHTML=xmlhttp.responseText;
}
</script>
<?php
include('dbconnection.php');
$country = $_GET["country"];
$state = $_GET["state"];
if($country!=""){
$sql2 = "select * from states where country_id= '$country'";
$result2 = mysqli_query($link11,$sql2);
echo "<select id ='statedd' onChange='change_state()'>";
while($row2=mysqli_fetch_array($result2)){
echo "<option value= '$row2[id]'>"; echo $row2['name']; echo "</option>";
}
echo "</select>";
}
?>
<?php
if($state!=""){
$sql2 = "select * from cities where stated_id '$state'";
$result2 = mysqli_query($link11,$sql2);
echo "<select>";
while($row2=mysqli_fetch_array($result2)){
echo "<option value= '$row2[id]'>"; echo $row2['name']; echo "</option>";
}
echo "</select>";
}
?>