Jump to content

Cannot send email Ajax Forms with jQuery


jalmz

Recommended Posts

Hi guys, im having problem in sending email using this code..

 

<?php
if(isset($_POST['submitted'])) {
if($_POST['emailTo'] == '') {
	$emailToError = 'You forgot to enter the email address to send to.';
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", $_POST['emailTo'])) {
	$emailToError = 'Enter a valid email address to send to.';
}
if($_POST['emailFrom'] == '') {
	$emailFromError = 'You forgot to enter the email address to send from.';
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", $_POST['emailFrom'])) {
	$emailFromError = 'Enter a valid email address to send from.';
}

if($_POST['subject'] == '') {
	$subjectError = 'You forgot to enter the subject.';
}

if($_POST['message'] == '') {
	$messageError = 'You forgot to enter the message.';
}

if(!isset($emailToError) && !isset($emailFromError) && !isset($subjectError) && !isset($messageError)) {
	include('sendemail.php');
	include('thanks.php');
}
}

?>
<form action="[color=red]sendemail.php[/color]" method="post" id="sendEmail">
	<h1>Send An Email</h1>
	<p class="alert">* All fields are required</p>
	<ol class="forms">
		<li><label for="emailTo">To</label><input type="text" name="emailTo" id="emailTo" value="<?= $_POST['emailTo']; ?>" /><?php if(isset($emailToError)) echo '<span class="error">'.$emailToError.'</span>'; ?></li>
		<li><label for="emailFrom">From</label><input type="text" name="emailFrom" id="emailFrom" value="<?= $_POST['emailFrom']; ?>" /><?php if(isset($emailFromError)) echo '<span class="error">'.$emailFromError.'</span>'; ?></li>
		<li><label for="subject">Subject</label><input type="text" name="subject" id="subject" value="<?= $_POST['subject']; ?>" /><?php if(isset($subjectError)) echo '<span class="error">'.$subjectError.'</span>'; ?></li>
		<li><label for="message">Message</label><textarea name="message" id="message"><?= $_POST['message']; ?></textarea><?php if(isset($messageError)) echo '<span class="error">'.$messageError.'</span>'; ?></li>
		<li class="buttons"><button type="submit" id="submit">Send Email »</button><input type="hidden" name="submitted" id="submitted" value="true" /></li>
	</ol>
</form>

 

sendemail.php

<?php

$to = "[email protected]";
$mailFrom = $_POST['emailFrom'];
$subject = $_POST['subject'];
$message = $_POST['message'];

  mail($to, $subject, $message);
?>

 

 

thanks.php

 

<h1>Success</h1>
<p>Your email was sent.</p>
<div class="clearing"></div>
</div>

</body>
</html>
<?php exit(); ?>

 

 

in the email form, im comfused..

 

<form action="sendemail.php" method="post" id="sendEmail">

 

what is the exact code  of form action? thanks :confused:

 

I've never used jQuery to send a form, but i recently did the same with some basic js and ajax.

 

Basically what happens:

js to cycles through the form elements, adding to a variable named 'params' the form elements name and value you require, which will be sent to your ajax processor via $_POST. The php will process the form data and echo back a response in the form of responseText:

 


function sendFormValues(){
/*xml connection*/
//Create an XMLHttpRequest object
if (window.XMLHttpRequest) {
	/*code for IE7+, Firefox, Chrome, Opera, Safari*/
	xmlhttp=new XMLHttpRequest();
}
else{
	/* code for IE6, IE5*/
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
/*setting the variables*/
//the url of the php file to process the data
var url = "http://www.mysite.net/ajaxProcessorA.php";
//what the post will hold. Added to with each element found in the loop below
var params = '';
var elem = document.getElementById('form').elements;
	for(var i = 0; i < elem.length; i++){
		elType = elem[i].type;
		elName = elem[i].name;
		elValue = elem[i].value;
		if(elType == 'radio'){
			/*check the radio button is selected before adding to the params*/
			if(elem[i].checked)
				{
					params = params + elName + "=" + elValue + "&";
				}
		}
		else if(elType == 'checkbox'){
			/*check the checkbox is checked before adding to the params*/
			if(elem[i].checked)
				{
					params = params + elName + "=" + elValue + "&";
				}
		}
		else if(elType == 'hidden'){
			/*no checks required just add to the params*/
			params = params + elName + "=" + elValue + "&";
		}
//keep adding here any other form elements you know you will need
	}

/*opening the connection*/
xmlhttp.open("POST", url, true);

/*Send the proper header information along with the request*/
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");

/*what to do with the response text*/
xmlhttp.onreadystatechange = function() {
	if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                //filling a container with the responseText from the server:
	document.getElementById('someDivID').innerHTML = xmlhttp.responseText;
	}
}
xmlhttp.send(params);
}

 

 

I know this isn't a direct answer to your question but hope it sheds some light...

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.