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?

<?php
// Error reporting:
error_reporting(E_ALL^E_NOTICE);

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

$redirectURL = 'http://www.myurl.com
$sender = $_POST['email'];
$name = $_POST['name'];
$projectDes = $_POST['info'];
$website = $_POST['website'];
$projectType = $_POST['project'];

$receiver = "myEmail@yahoo.com";
$client_ip = $_SERVER['REMOTE_ADDR'];

$siteName = "www.sitename.com";

$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):

if($_POST['fromAjax'])
{
if(count($errors))
{
$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:
$errString[]='"'.$k.'":"'.$v.'"';
}

// 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.'"}';
}
else
{
echo "success=no";
}



exit;
}

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

if(count($errors))
{
echo '<h2>'.join('<br /><br />',$errors).'</h2>';
exit;
}

// Directly redirecting the visitor:

header("Location: ".$redirectURL);
?>
anthonyv1121
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: No registered users and 1 guest

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

cron