posting to php through ajax

Posted on 2009-04-03
Last Modified: 2013-12-12
I am stumpped on posting variables to a php file.
From my html I call 'calledFromHTMLform()'.  This file generates an ajax variable and posts to a file called 'myPHPfile.php'.
The php file sends an email. The email is successfully sent but I cannot read any of the posted variables.
In the following code, the $message variable contains nothing.
Any input as to what I am missing?


function calledFromHTMLform() { 

	var ajaxVar = getAjaxRequestVariable(); 

	ajaxVar.onreadystatechange = function() { 

		if(ajaxVar.readyState == 4) { 

			var myResponse = ajaxVar.responseText; 


	}"POST", "myPHPfile.php", true); 




function getAjaxRequestVariable() { 

	try { // Opera 8.0+, Firefox, Safari 

		ajaxRequest = new XMLHttpRequest(); 

	} catch (e) { // Internet Explorer Browsers 

		try { 

			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); 

		} catch (e) { 

			try { 

				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); 

			} catch (e) { // Something went wrong 

				alert("Your browser broke!"); 

				return false; 




	return ajaxRequest; 




	$email = '';

	$subject = 'This is the subject';

	$message = $_POST["name"]; 

	$from = "";

	$headers = "From: $from";

	if(mail($email,$subject,$message,$headers)) {

		echo "Thank you for sending email";

	} else {

		echo "Can't send email to $email";




Open in new window

Question by:pconder
  • 2

Expert Comment

ID: 24064479
Are you certain these variables are being populated correctly?


Dump them to the screen to ensure they have data.

Author Comment

ID: 24064602
I did that already. The string going to the post looks good (I think). The following is a sample string being sent to the post from the actual code - not the stripped and hard coded code that I posted with the question:

Accepted Solution

Pantalaim0n earned 500 total points
ID: 24066932
Try adding this before ajaxVar.send(...). Replace lengthOfTheQueryString with an appropriate value.

ajaxVar.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

ajaxVar.setRequestHeader('Content-Length', lengthOfTheQueryString);

Open in new window


Author Closing Comment

ID: 31566444
Someone also recomended that I add:
ajaxVar.setRequestHeader("Connection", "close");
after the two lines you indicated.  Works great. Thanks.

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now