Im a beginner with php and Javascript. I am trying to make an Ajax request using a JavaScript function to display the country description based on the form text input(country name). The response should come from a .php file which takes in country name as a parameter and produce a response that will consist of a brief description of that country when the user clicks the button. Below is my html code:
<html>
<head>
<script>
/
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest)
{
XMLHttpRequestObject = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
function getData(dataSource, divID)
{
if(XMLHttpRequestObject)
{
var obj = document.getElementById(divID);
obj.innerHTML ="";
XMLHttpRequestObject.open("GET", dataSource);
XMLHttpRequestObject.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
XMLHttpRequestObject.onreadystatechange =
function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200)
{
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}
XMLHttpRequestObject.send(null);
}
}
</script>
</head>
<body>
<h1 align="center">Country Description</h1>
<form>
<p>Country Name: <input type="text" name="country">
<input type="button" value="Show Description" onclick="getData('hw5a.php', 'targetDiv')">
</p>
</form>
<div id="targetDiv">
<p>The response will display here</p>
</div>
</body>
</html>
Following is my php code:
<?php
header("Cache-Control: post-check=1,pre-check=2");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
$c = $_GET['country'];
$c = strtolower($country);
echo $country;
if($c = 'canada')
{
echo '<p>canada</p>';
}
elseif($c = 'usa')
{
echo '<p>USA</p>';
}
elseif($c = 'mexico')
{
echo '<p>c</p>';
}
else
echo '<p>Please check your spellings</p>'
?>
I am not sure what am i doing wrong. Any help or direction will be appreciated. Thanks
</div>