AJAX Send Email Form with prototype and PHP
Hey Im trying to send an email with PHP. but Im using ajax to load all my pages So i dont want to disrupt that flow by having the page refresh when you hit submit..I just want it to be like loading and then echo Thank you or something. Here is my current code.
HTML
<form method"post" onsubmit="return false;">
<label>Your Email:<input type="text" name="email" id="email" /></label>
<label>Subject:<input type="text" name="subject" id="subject" style="width:500px;" /></label>
<label>Message:<input type="text" name="message" id="message" style="width:500px;height:200px;background-color:#DFB782;color:#000;border:1px solid #462401;font-weight:bold;" /></label>
<input type="submit" value="Send" style="background-color:#DFB782;color:#000;border:1px solid #462401;font-weight:bold;margin-top:5px;padding:5px;font-size:18px;" onClick="sendRequest()" />
</form>
PHP
<?php
$mail['to'] = 'business@caffeinatedcustoms.com';
$mail['from'] = $_POST['email'];
$mail['subject'] = $_POST['subject'];
$mail['message'] .= $_POST['message'];
$mail['header'] = 'From: '.$_POST['Name'].'<'. $mail['from'] . ">\r\n";
$mail['status'] = @mail($mail['to'], $mail['subject'], $mail['message'], $mail['header']);
if ($mail['status'] == false) {
echo 'Failed to send mail to ' . $mail['to'] . '.Error: ' . error_get_last();
exit;
}
else{
echo 'Thank You we will Resond Soon';
}
?>
Javascript
<script type="text/javascript" src="prototype.js"></script>
<script>
function sendRequest() {
new Ajax.Request("sendmail.php",
{
method: 'post',
postBody: 'message='+ $F('message'),
onComplete: showResponse
});
}
function showResponse(req){
$('show').innerHTML= req.responseText;
}
</script>
Thanks in advance ;)
mikeandike22
Nearly a Posting Virtuoso
1,496 posts since May 2004
Reputation Points: 33
Solved Threads: 19
This forum is basically for generic Javascript questions which don't necessarily refer to a particular library. This question would be more appropriate in the Prototype.js forums where you have better chances of getting help. :-)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733