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 = "[email protected]";
$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
https://forums.phpfreaks.com/topic/286711-email-problems/
Share on other sites

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.