# Problem with sending form data with Ajax POST

**URL:** <https://forum.kirupa.com/t/problem-with-sending-form-data-with-ajax-post/299987>\
**Category:** web dev\
**Created:** [November 13, 2009, 6:56pm UTC](https://forum.kirupa.com/t/problem-with-sending-form-data-with-ajax-post/299987 "2009-11-13T18:56:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![magnusvs](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@magnusvs](https://forum.kirupa.com/u/magnusvs)\
**Post date:** [November 13, 2009, 6:56pm UTC](https://forum.kirupa.com/t/problem-with-sending-form-data-with-ajax-post/299987/1 "2009-11-13T18:56:22Z")

</div>

Hi,

I have a problem with sending data from a form to a php script with AJAX. To test if it works, I try to send data from the form, print it in the php with “echo”, and then put it back in the initial html file.

My Javascript code is:

```auto
function registerPrivateUser()
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	}
	
	var postData="firstName="+ document.getElementById("txtFirstName").value;
	var url="classes/users/checkRegistration.php";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("POST",url,true);
	//xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
	
	xmlhttp.send(postData);
}

```

The function stateChanged, basically says:

```auto
if (xmlhttp.readyState==4)
	{
		var strResponse;
		strResponse=xmlhttp.responseText;
		alert('Response:' + xmlhttp.responseText);
	}

```

checkRegistration.php looks like this:

```php
<? 
session_start(); 
$firstName=$_POST['firstName']; 
echo($firstName); 
?>

```

The problem is that the response is empty, but I don’t know why. I have checked the input data and the postData variable says “firstName=”+input (e.g. “firstName=Robert”), so that’s not the error. I have read several forum posts here, but still haven’t figured out what I’m doing wrong.  
If anyone could please tell me, I would appreciate it alot.
