Go Back   kirupaForum > Development > Client-Side (HTML, CSS, JavaScript)

Reply
 
Thread Tools Display Modes
Old 11-13-2009, 02:56 PM   #1
magnusvs
Registered User
Problem with sending form data with Ajax POST

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:
Code:
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:

Code:
if (xmlhttp.readyState==4)
	{
		var strResponse;
		strResponse=xmlhttp.responseText;
		alert('Response:' + xmlhttp.responseText);
	}
checkRegistration.php looks like this:

PHP Code:
<? 
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.
magnusvs is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-14-2009, 04:22 PM   #2
a tadster
tadster
 
a tadster's Avatar
hmmm.. perhaps the php is not running at all?
maybe get rid of the session start.

you should include the content type header. (try without the "; charset...")

where is the stateChanged function?

also try:
...
var firstName=""+ document.getElementById( ' txtFirstName ' ).value+"";
...
xmlhttp.send(firstName);

__________________
www.actiontad.com
a tadster is offline   Reply With Quote
Old 11-18-2009, 03:22 PM   #3
bassmansg
Registered User
 
bassmansg's Avatar
Since you are using POST you should set your request headers.

Something like this should work...

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");
xmlhttp.setRequestHeader("Content-Length", postData.length);
xmlhttp.setRequestHeader("Connection","close");

xmlhttp.send(postData);
}

__________________
<!--0.o--> don't tell the elf
bassmansg is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:18 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com