Solved

posting to php through ajax

Posted on 2009-04-03
4
308 Views
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; 
		} 
	} 
	ajaxVar.open("POST", "myPHPfile.php", true); 
	ajaxVar.send('name=myname&username=myusername'); 
} 
--------------------------------------------------------------------------------
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; 
} 
--------------------------------------------------------------------------------
<?php
	$email = 'you@domainname.com';
	$subject = 'This is the subject';
	$message = $_POST["name"]; 
	$from = "me@domainname.com";
	$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

0
Comment
Question by:pconder
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

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

ajaxVar.send('name=myname&username=myusername');

Dump them to the screen to ensure they have data.
0
 

Author Comment

by:pconder
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:
------------------------------------------------
name=pc&username=myUser
------------------------------------------------
0
 
LVL 5

Accepted Solution

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

0
 

Author Closing Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 8 91
Php logic to add to date card 9 38
How to use session variables in php? 22 43
Json and ajax 1 19
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

735 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