AJAX and Php not keeping values between my drop downs

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>";
		}
?>

Only one of your two Ajax calls sends the state parameter to your PHP, so it’s not surprising that the PHP cannot find it and gives a warning. It hasn’t “lost” it, it just never sent it in the first place. The surprise is that you don’t get a similar error message about “country” when you change the state. You should really check whether the parameters exist before using them.

This query doesn’t look correct to me, does it run without an error?

$sql2 = "select * from cities where stated_id '$state'";

Shouldn’t this

echo "<select>";

have a name on it, so you can access the city selection?

Fixed the Query

it actually works it gives me Country, State and City. the value is correct but between each dropdowns it still gives
Undefined index: state in C:\xampp\htdocs\loginandout\testandlearnajax2.php on line 5 and
|Undefined index: country in C:\xampp\htdocs\loginandout\testandlearnajax2.php on line 4|

Also not sure how i can fix the matter you raised by saying only one of the two AJAX sends the to my PHP. pretty new to AJAX :(
<?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!=""){

			$sql3 = "select * from cities where stateid ='$state'";

			$result3 = mysqli_query($link11,$sql3);

			echo "<select id ='statedd' onChange='change_state()'>";

			while($row3=mysqli_fetch_array($result3)){

			echo "<option value= '$row3[id]'>"; echo $row3['name']; echo "</option>";


						}
		echo "</select>";
		}
?>

Well, it will do. This piece of code:

$country = $_GET["country"];
$state = $_GET["state"];

uses one parameter that exists, and one that does not exist - which is which varies between your two calls. So it complains about the one that does not exist. You need to either use isset() to see if each parameter exists before you use it, or change your links to provide both parameters, even if the second one is empty.

Thanks alot for the help. I now understand the use of ISSET() better aswell.

Thanks again appreciated