Jordan U. Jordan U. - 3 months ago 11
PHP Question

Run PHP Code After Form Submission And Redirect

I want to run some php code after a form is submitted on my website. It redirects to another page to email the information, then uses header() to return to the previous page. What I would like to happen is to load a small message that shows that the message was sent successfully, but I'm not sure how to do this after being redirected from another page. I'm hoping to use php or jquery to accomplish this, and I'm including the code I'm using below. I have tried to use the session_start function to do this, but the echoed text remains on the page after the user leaves.

<form method="post" action="mail.php">
<tr>
<td class="label">Name:</td>
<td class="input"><input type="text" maxlength="40" name="name" pattern="[a-zA-Z0-9]+" title="Please enter your name." required></td>
</tr>
<tr>
<td class="label">Email:</td>
<td class="input"><input type="email" maxlength="24" name="email" title="Please enter an email address." required></td>
</tr>
<tr>
<td class="label">Subject:</td>
<td class="input"><input type="text" maxlength="24" name="subject" title="Please enter a subject." required></td>
</tr>
<tr>
<td class="label">Message:</td>
<td class="input"><textarea rows="9" maxlength="1000" name="message" title="Please enter a message." required></textarea></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</form>

<?php
$to = "kouen922@gmail.com";
$subject = "Message From Your Website: ".trim(htmlspecialchars($_POST["subject"]));
$message = trim(htmlspecialchars($_POST["message"]));
$headers = "Reply To: ".trim(htmlspecialchars($_POST["email"]))."" ."\r\n". "From: " .trim(htmlspecialchars($_POST["name"]))."";
mail($to,$subject,$message,$headers);
header("Location: index.php#contact");
?>

Answer

Easy.

header("Location: index.php?thankyou#contact");

Then do a conditional for isset($_GET['thankyou']) and print out a message.

Comments