Jump to content

Email problems


kayz100

Recommended Posts

Hi guys

How do I get a thank you message on the same page instead of redirecting the message to a different page as on the script here - 

header('Location: thanks.html');

 

Also I want the errors to be echoed in front of the field concerned as in name, email, subject, message

<?php
$myemail = "hila@gmail.com";
$name = check_input($_POST['name'], "Enter your name"); //error or success message echoed here
$subject = check_input($_POST['subject'], "Enter a subject");//error or success message echoed here
$email = check_input($_POST['email']);//error or success message echoed here
$message = check_input($_POST['message'], "Write your message");//error or success message echoed here

if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}

$message = "
Name: $name
E-mail: $email
Subject: $subject


Message:
$message


";

mail($myemail, $subject, $message);

//This is where the problem is
header('Location: thanks.html'); //Want to have this message on the same page 
exit();


function check_input($data, $problem=''){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0){
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>

<html><head>
<title> email 2014</title>
</head>

<body>
<strong><?php echo $myError; ?></strong><br /><br />
<form class="email" action="" method="post">
<p>Name:</p>
<input type="text" name="name" />
<p>E-mail:</p>
<input type="text" name="email" />
<p>Subject:</p>
<input type="text" name="subject" />
<p>Message:</p>
<textarea name="message"></textarea></p>
<input class="send" type="submit" value="Send">
</form>
<?php exit(); } ?>

</body></html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.