PHP Redirect to new page after form submission

前端 未结 5 806
不知归路
不知归路 2021-01-23 21:51

I have a form, which redirects the user to \"page1.php\" after the form is submitted. What I want to do is to redirect the user to \"page2.php\" after the form is submitted, but

相关标签:
5条回答
  • 2021-01-23 22:36

    In your 'page1.php' processor, add a 'header' redirect to 'page2.php'.

    header("Location: page2.php");
    exit;
    
    0 讨论(0)
  • 2021-01-23 22:38

    In your situation, you can just simple check for the posted data. Like

    $username = $_POST['username'];
    $age = $_POST['age'];
    if($username&&$age){ /* This is to check if the variables are not empty  */
     // redirect to page2
    }
    

    It is logical that if those are not empty, meaning they are posted. Even if they are empty, as long as you get there, that means it was posted. There is no need to check if posted or not, what needs to be checked was, if posted data was there.

    I hope I made it clear. ^_^ but making sure is not bad at all. Happy coding friend.

    0 讨论(0)
  • 2021-01-23 22:47

    I guess this works !! In page1.php

    <?php
    //do establish session
    //and check for the input fields obtained via $_POST
    if(isset($_POST['name_of_your_field']) && !empty($_POST['name_of_your_field'])){
        if(!mail($to,$subject,$message)){
            header('location:form.php?msg=error');  
        }else{
            header('location:page2.php?msg=succes');
            }
    }   
    ?>
    
    0 讨论(0)
  • 2021-01-23 22:54

    You can check if the POST request was sent with something like:

    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        // do something...
    }
    

    You can create a hidden input in your form and send additional info about the form that is submitted, e.g. action.

    Inside you will do your magic and redirect user with:

    header('Location: page2.php');
    exit();
    
    0 讨论(0)
  • 2021-01-23 22:54

    you could do a check if query is complete

    example

    <?php
    $query=mysqli_query(".......your query statement")or trigger_error(mysqli_error());
    if($query){
               header("Location:page2.php");
              }
    else{
         die('error');
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题