Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Mail

Return form sent message - no refresh

the mail() function

Moderators: macek, egami, gesf

Return form sent message - no refresh

Postby anthonyv1121 » Thu Jun 09, 2011 9:33 am

I have a simple html contact form validating with ajax, instead of having a redirect to another url when the form is submitted, I'd like to display a simple message on screen that the message has been sent. Here's my script, any help please?

// Error reporting:

// This is the URL your users are redirected,
// when registered succesfully:

$redirectURL = '
$sender = $_POST['email'];
$name = $_POST['name'];
$projectDes = $_POST['info'];
$website = $_POST['website'];
$projectType = $_POST['project'];

$receiver = "";
$client_ip = $_SERVER['REMOTE_ADDR'];

$siteName = "";

$autoReply = "From: $receiver\r\n" . "Reply-To: $receiver \r\n" . "X-Mailer: PHP/" . phpversion();
$extra = "From: $sender\r\n" . "Reply-To: $sender \r\n" . "X-Mailer: PHP/" . phpversion();

$emailAutoReply = "Hi $name, \n\nThank you. \n\nSincerely, \n\n$siteName ";

$email_body = "The Name Of The Sender: $name \n\nEmail: $sender \n\nCurrent Website: $website";

$errors = array();

// Checking the input data and adding potential errors to the $errors array:

if(!$_POST['name'] || strlen($_POST['name'])<3 || strlen($_POST['name'])>50)
$errors['name']='Please provide your name. Must be between 3 and 50 characters.';

if(!$_POST['email'] || !preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email']))
$errors['email']='Please provide a valid email address!';

if(!$_POST['info'] || strlen($_POST['info'])<30)
$errors['info']='Please provide some information';

// Checking whether the request was sent via AJAX
// (we manually send the fromAjax var with the AJAX request):

$errString = array();
foreach($errors as $k=>$v)
// The name of the field that caused the error, and the
// error text are grouped as key/value pair for the JSON response:

// JSON error response:
die ('{"status":0,'.join(',',$errString).'}');

mail( $sender, "Auto-reply", $emailAutoReply, $autoReply );

// JSON success response. Returns the redirect URL:
if( mail( $receiver, "Subject Title", $email_body, $extra ) )

echo '{"status":1,"redirectURL":"'.$redirectURL.'"}';
echo "success=no";


// If the request was not sent via AJAX (probably JavaScript
// has been disabled in the visitors' browser):

echo '<h2>'.join('<br /><br />',$errors).'</h2>';

// Directly redirecting the visitor:

header("Location: ".$redirectURL);
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Jun 09, 2011 9:26 am

Return to PHP coding => Mail

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.