Jump to content

[SOLVED] PHP and AJAX post request


rvdb86

Recommended Posts

hey guys hope some one can help me out.

 

I am submitting a form through ajax. The file sendform.php processes the data sent and sends it as an email. The problem is that the user enters their email address in to the email field on the form, which is used as the sender value in the email, but for some reason $_POST['email'] is blank.  ???

 

This is the AJAX part that send the form data to sendform.php:

makePOSTRequest('Scripts/sendform.php', poststr);

 

Any ideas would be really appreciated!

Link to comment
https://forums.phpfreaks.com/topic/162383-solved-php-and-ajax-post-request/
Share on other sites

Sorry,

 

This is the javascript:

   var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('contact-cube').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   
   function get(obj) {
      var poststr = "Name=" + escape(encodeURI(document.getElementById("name").value )) 
  	+"&Telephone=" +
	escape(encodeURI( document.getElementById("telephone").value ))
	+"&Email=" +
	escape(encodeURI( document.getElementById("email").value ));


      makePOSTRequest('Scripts/sendform.php', poststr);
   }

 

And this is the sendform.php:

 

require_once "Mail.php";

$from = $_POST['name'] . " <".$_POST['email'].">";
$to = "Me <me@mel>";
$subject = "Subject";
$body = "Hi,\n\nHow are you?";

$host = "mail.host.co.il";
$username = "Usernamel";
$password = "Password";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);
$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}

Sorry miss read... here is the code that sets postr:

 

var poststr = "Name=" + escape(encodeURI(document.getElementById("name").value )) 
  	+"&Telephone=" +
	escape(encodeURI( document.getElementById("telephone").value ))
	+"&Email=" +
	escape(encodeURI( document.getElementById("email").value ));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.