Jordan U. Jordan U. - 1 year ago 62
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">
<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>
<td class="label">Email:</td>
<td class="input"><input type="email" maxlength="24" name="email" title="Please enter an email address." required></td>
<td class="label">Subject:</td>
<td class="input"><input type="text" maxlength="24" name="subject" title="Please enter a subject." required></td>
<td class="label">Message:</td>
<td class="input"><textarea rows="9" maxlength="1000" name="message" title="Please enter a message." required></textarea></td>
<input type="submit" value="Submit">

$to = "";
$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"]))."";
header("Location: index.php#contact");

Answer Source


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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download