Wednesday, July 4, 2012

Redirect webpage to the same page with a message


Redirect webpage to the same page with a message




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:


Code:
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:




Code:
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>'
    }
}  



No comments:

Post a Comment