AJAX PHP/Javascript and Form Data Issue

software development

#1

I am trying to get a php file to check some data from a form. Right now I just testing if I can send a php file data and it can parrot it back.

My guess is that the problem is with the xmlhttp.open to send part but I am fairly new to AJAX and PHP. The javascript does get the data from the form I have checked that part. When I click on the button nothing happens.

var xmlhttp = new getXMLObject();	

function getXMLObject(){
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest(); //Mozilla, Safari ...
	} else if (window.ActiveXObject) {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE 6
	} 
}

function ajaxFunction(form) {
	var url = "parrotBack.php";
	var name=form.name.value;
	var data = "name="+name;
	if(xmlhttp) { 
		xmlhttp.open("POST",url,true);
    	xmlhttp.onreadystatechange  = handleServerResponse;
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		//xmlhttp.setRequestHeader("Content-length", data.length);
		//xmlhttp.setRequestHeader("Connection", "close");
    	xmlhttp.send(data);
  }

}


function handleServerResponse() {
   if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      document.getElementById("result").innerHTML=xmlhttp.responseText;      }
     else {
        txt = "Error during AJAX call. Please try again";
		document.getElementById("result").innerHTML=txt;
     }
}

The PHP code

<?
$name = $_POST['name']; 
echo $name;
?>

Here is the HTML section too

<form id="frmMail" name="frmMail" action="">
	<table>
    	<tr><td>Name: </td>
        <td><input name="name" type="text" size="30" /></td>
        </tr>
        <tr>
	<td colspan="2"><input name="" type="button" value="Send Feedback" onclick="ajaxFunction(document.frmMail)"/></td>
        </tr>		
     </table>
	<div id="result">&nbsp;</div>
</form>