Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Ajax Problem!

AJAX ajax

  • Please log in to reply
6 replies to this topic

#1 JackomoLight

JackomoLight

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 97 posts
  • Programming Language:PHP, Python, JavaScript, PL/SQL
  • Learning:C, Java, C++, Perl, Ruby, Lisp, Bash

Posted 29 April 2012 - 12:53 PM

Hello guys, I am trying to write a script that sends e-mail using AJAX for convenience. However there seems to be a problem which cannot identify for quite a while now so have a look at it if you like, any help is greatly appreciated. Just bare in mind also that I am using lots of jQuery

Here is the code, first php:


<?php

$name  = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$eventdate = $_POST['eventdate'];
$eventlocation = $_POST['eventlocation'];
$eventtype = $_POST['eventtype'];
$peoplenumber = $_POST['peoplenumber'];
$comments = $_POST['comments'];

$msg = '<table border="1" cellspacing="0" cellpadding="2" bordercolor="#000000" width="100%"><tbody>';
$msg .= '<tr><td bgcolor="#999999">Name:</td><td>' . $name  . '</td></tr>';
$msg .= '<tr><td bgcolor="#999999">E-mail:</td><td>' . $email  . '</td></tr>';
$msg .= '<tr><td bgcolor="#999999">Phone:</td><td>' . $phone  . '</td></tr>';
$msg .= '<tr><td bgcolor="#999999">Event date:</td><td>' . $eventdate  . '</td></tr>';
$msg .= '<tr><td bgcolor="#999999">Event location:</td><td>' . $eventlocation  . '</td></tr>';
$msg .= '<tr><td bgcolor="#999999">Event type:</td><td>' . $eventtype  . '</td></tr>';
$msg .= '<tr><td bgcolor="#999999">People number:</td><td>' . $peoplenumber  . '</td></tr>';
$msg .= '<tr><td bgcolor="#999999">Comments:</td><td>' . $comments  . '</td></tr>';
$msg .= '</tbody></table>';

$to = "my.email@gmail.com";

$subject = "New Message Left for You";

$headers = "From: bookings@domain.com";

if (mail($to, $subject, $msg, $headers) ) {
  echo "Message successfully sent.";
} else {
echo "Message failed. Please try again.";
}
?>

As far as I know the PHP works just fine, cuz when I go straight to php file it send an empty e-mail.

Here is the javascript/ajax function:

function sendMail() {

$(function() {

  name = $('#name').val();
  email = $('#email').val();
  phone = $('#phone').val();
  eventdate = $('#eventdate').val();
  eventlocation = $('#eventlocation').val();
  eventtype = $('#eventtype').val();
  peoplenumber = $('#peoplenumber').val();
  comments = $('#comments').val();

  $('#wait').show();
  $('#notice').text("");
  $('#tick').hide();

  vars = 'name=' + name + '&email=' + email + '&phone=' + phone + '&eventdate=' + eventdate + '&eventlocation='+ eventlocation + '&eventtype=' + eventtype + '&peoplenumber=' + peoplenumber + '&comments=' + comments;
  xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange=function()
	{
	 if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
	   {
	  if ( xmlhttp.responseText=='Message successfully sent.' )
	  {
	   $('#wait').hide();
	   $('#tick').show();
	   $('#notice').html( xmlhttp.responseText );
	  }
	  else
	  {
		if ( xmlhttp.responseText.length > 60 ) {
		 $('#wait').hide();
		 $('#notice').html("Message failed. Please try again.");
		}
		else {
		 $('#wait').hide();
		 $('#notice').html(xmlhttp.responseText);
		}
	  }
	
	   }
	}

  xmlhttp.open("POST","mail.php",true);
  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xmlhttp.send(vars);

});
}

for any case here is the form:

<div id="forms">
					
<input type="text" name="name" class="form-input" id="name" /><br />
<input type="text" name="email" class="form-input" id="email" /><br />
<input type="text" name="phone" class="form-input" id="phone" /><br />
<hr class="line2" />
<input type="text" name="eventdate" class="form-input" id="eventdate" /><br />
					  
<input type="text" name="eventlocation" class="form-input" id="eventlocation" /><br />
<input type="text" name="eventtype" class="form-input" id="eventtype" /><br />
<input type="text" name="peoplenumber" class="form-input" id="peoplenumber" /><br />
<textarea name="comments" rows="4" cols="35" id="comments">
					  
</textarea><br />
<input type="button" value="SEND" id="submit-button" onclick="sendMail()" />
<img src="images/wait2.gif" class="imgs" id="wait"  />
<img src="images/tick2.png" class="imgs" id="tick"  />
<normal id="notice"></normal>
					  
</div>

  • 0

#2 JackomoLight

JackomoLight

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 97 posts
  • Programming Language:PHP, Python, JavaScript, PL/SQL
  • Learning:C, Java, C++, Perl, Ruby, Lisp, Bash

Posted 29 April 2012 - 01:21 PM

An update: when I remove
xmlhttp.status == 200

the script comes out with the:

"Message failed. Please try again."

message

Update 2: thats the actual error that's raised when I remove: xmlhttp.status == 200


"An appropriate representation of the requested resource /temp/mail.php could not be found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request"

Apparently it has something to do with the server, just changed the permissions still nothing.
  • 0

#3 JackomoLight

JackomoLight

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 97 posts
  • Programming Language:PHP, Python, JavaScript, PL/SQL
  • Learning:C, Java, C++, Perl, Ruby, Lisp, Bash

Posted 29 April 2012 - 02:00 PM

Alright guys, just tried to do the same without ajax just standard html form and I get the same error, on top of that while I am still on the same page(with the error) I go to the link field of my browser press enter and the script sends the e-mail !
  • 0

#4 JackomoLight

JackomoLight

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 97 posts
  • Programming Language:PHP, Python, JavaScript, PL/SQL
  • Learning:C, Java, C++, Perl, Ruby, Lisp, Bash

Posted 29 April 2012 - 02:37 PM

Finally solved, but still have some problems, however I can use the form now and it sends the e-mail but without any of the information.

the second solution on link did it, if you are interested: http://www.tipsandtr...table-issue-259

So I am still looking for some help, thanks in advance.


I am currently completely baffled somehow the php script doesn't receive the input or the form fails to post it, or I am missing something, or there is something wrong with the server again, or I need some sleep!
  • 0

#5 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 29 April 2012 - 10:38 PM

Hey guy !
give the people time to come and check your problem ! i come and you have already made and eaten the soup !

could you please define better the situation :

i mean, did you check what the php receives and what it does not ? is it one variable or is it many ?
thing is , you have a textarea there, by example and you might need to urlencode it, and all the data you punt into "vars" string
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 30 April 2012 - 12:57 AM

  • What browser do you use? Because chrome and firefox got nice plugins to send post requests - easy for testing
rest.png
  • Why don't you use JQuery ajax() ?
  • Do you get a 404 if you access your mail.php page in the browser?

  • 0

#7 JackomoLight

JackomoLight

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 97 posts
  • Programming Language:PHP, Python, JavaScript, PL/SQL
  • Learning:C, Java, C++, Perl, Ruby, Lisp, Bash

Posted 30 April 2012 - 03:19 AM

Well I just tried using just standard html form and the "GET" method. Here is the link I am trying:

mail.php?name=name&email=email&phone=daslk&eventdate=%3Blkldjaskl&eventlocation=kjdasqw&eventtype=qweqwe&peoplenumber=qweqwe&comments=12321321312312

and I put some print statements into the mail.php and it just prints "Message successfully sent" therefore I still receive the e-mail without any of the input. Also I just tested it on my local server and it works just fine, it prints the input! I guess the problem has something to do with the hosting provider?!!! Which is http://www.justhost.com
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download