PDA

View Full Version : PHP how to redirect to the same page with a message



angelwim
November 17th, 2009, 07:38 AM
hi

I am trying to use the kirupas great php tutorial to submit a contact form. But when the submit button is pressed , i want it to go to a certain HTML file (the same form file with just an additional line of 'THANK YOU. YOUR DATA HAS BEEN SUBMITTED' etc....).

how do i get this done in PHP. I am new to this and any help put in simple way will be highly appreciated.

actionAction
November 17th, 2009, 10:42 AM
Are your processing the form on the same page or is the form action a separate file? If it is a separate file, you can redirect after form processing using the header function and passing in a get variable like so:



/* PASTE THIS BELOW ALL OF YOUR FORM PROCESSING */
if($form_success){
header('original_page.php?success=true');
}

Here I am checking a made-up variable called $form_success, though it would probably contain a boolean value based on whether the form successfully submitted or not. If it did submit, I am redirecting the user to original_page.php with a GET variable success = true. This can be accessed on your form page (wherever you want the message to display in the HTML) like this:



if(isset($_GET['success'])){
if($_GET['success'] == 'true'){
echo '<div class="success">THANK YOU. YOUR DATA HAS BEEN SUBMITTED</div>'
}else{
echo '<div class="error">SORRY, PROBLEM SUBMITTING YOUR DATA</div>'
}
}