表单提交后重定向用户

Redirect User after Form Submit

I have a PHP page with a form and after the user completes the form I would like to redirect them to a new page where they can decide their next action.

我看到的建议涉及更改 Form Action,但是我的代码已经有一个 <?php $_PHP_SELF ?> 的操作。我是 PHP 的新手,所以也许我可以用 PHP_Self?

之外的其他方法提交表单数据

下面的代码,提前感谢您的帮助,也欢迎任何有关简化我的代码的建议

    <div class="Form_container">
        <form method="post" action="<?php $_PHP_SELF ?>" name="interactionForm">
        <hr>
        <span class="SectionHeader">Business Information</span><br>
        <hr>
        <br>
            DBA<br>
            <input class="largeInput" type="text" name="DBA" ID="DBA"><br>
            Use<br>
            <input class="largeInput" type="text" name="BusinessUse" ID="BusinessUse"><br>
            Street Address<br>
            <input class="largeInput" type="text" name="StreetAddress" ID="StreetAddress"><br>
            City<br>
            <input class="largeInput" type="text" name="City" ID="City"><br>
            State<br>
            <input class="largeInput" type="text" name="State" ID="State"><br>
            Zip<br>
            <input class="largeInput" type="number" min="1" max="99999" name="Zip" ID="Zip"><br>
            Number of Locations<br>
            <input class="largeInput" type="number" min="1" max="99999" name="NumberOfLocations" ID="NumberOfLocations"><br>
            Size (sq ft)<br>
            <input class="largeInput" type="number" min="1" max="9999999" name="LocationSize" ID="LocationSize"><br>
        <hr>
        <span class="SectionHeader">Contact Information</span><br>
        <hr>
        <br>
            Contact First Name<br>
            <input class="largeInput" type="text" name="contactFirstName" ID="contactFirstName"><br>
            Contact Last Name<br>
            <input class="largeInput" type="text" name="contactLastName" ID="contactLastName"><br>
            Contact Title<br>
            <input class="largeInput" type="text" name="ContactTitle" ID="ContactTitle"><br>
            Contact Phone Number<br>
            <input class="largeInput" type="text" name="contactPhoneNumber" placeholder="### - ### - ####" ID="contactPhoneNumber"><br>
            Contact Email<br>
            <input class="largeInput" type="text" name="contactEmail"><br>
            Initial Method of Contact<br>
            <select style='text-align:center;' class='largeInput' name='InitialContactMethod'>
                <option value="Phone">Phone</option>
                <option value="Door">Door</option>
                <option value="Referral">Referral</option>
            </select><br>
            <!-- Small Date picker --><!--Last Contact Date<br> 
            <input class="largeInput" type="date" name="lastContactDate"><br>-->
        <hr>
        <span class="SectionHeader">Showing Information</span><br>
        <hr>
        <br>
            Property<br>
            <select style='text-align:center;' class='largeInput' name='PropertyID'>
                <?php
                    $servername = "localhost";
                    $username = "usernameTest";
                    $password = "passwordTest";
                    $dbname = "databaseTest";

                    // Create connection
                    $conn = new mysqli($servername, $username, $password, $dbname);

                    // Check connection
                    if ($conn->connect_error) {
                        die("Connection failed: " . $conn->connect_error);
                    }   
                    //Option Field
                    $PropertySelect = "SELECT DISTINCT PropertyName, PropertyID FROM Property ORDER BY PropertyName DESC";
                    $PropertyOptions = $conn->query($PropertySelect);

                    if ($PropertyOptions->num_rows > 0) {
                        // output data of each row
                        while($row = $PropertyOptions->fetch_assoc()) {
                            echo "<option value='". $row["PropertyID"]. "'>" . $row["PropertyName"]. "</option><br><br>";   
                        }
                    } else {
                        echo "0 results";
                    }
                    mysqli_close($conn);
                ?>
            </select><br>
            Quality of Lead<br>
            <select style='text-align:center;' class='largeInput' name='QualityOfLead'>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="7">7</option>
                <option value="8">8</option>
                <option value="9">9</option>
                <option value="10">10</option>
                <option value="LOI">LOI</option>
                <option value="AtLease">At Lease</option>
                <option value="Executed">Executed</option>
            </select><br>
            Notes<br>
            <textarea name="Notes" cols="60" rows="10" maxLength="4000;"></textarea><br/>
            <!--<input type=hidden name="redirect" value="http://www.leasinglog.com/entrycomplete.php">-->


        <?php
     if(isset($_POST['add']))
    {
            $con=mysqli_connect("localhost","usernameTest","passwordTest","databaseTest");
            // Check connection
            if (mysqli_connect_errno())
              {
              echo "Failed to connect to MySQL: " . mysqli_connect_error();
              }

            if(! get_magic_quotes_gpc() )
        {
            //CONTACT FIELDS
            $contactFirstName = addslashes ($_POST['contactFirstName']);
            $contactLastName = addslashes ($_POST['contactLastName']);
            $ContactTitle = addslashes ($_POST['ContactTitle']);
            $contactPhoneNumber = addslashes ($_POST['contactPhoneNumber']);
            $contactEmail = addslashes ($_POST['contactEmail']);
            $InitialContactMethod = addslashes ($_POST['InitialContactMethod']);
            //RETAILER FIELDS
            $DBA = addslashes ($_POST['DBA']);
            $BusinessUse = addslashes ($_POST['BusinessUse']);
            $StreetAddress = addslashes ($_POST['StreetAddress']);
            $City = addslashes ($_POST['City']);
            $State = addslashes ($_POST['State']);
            $Zip = addslashes ($_POST['Zip']);
            $NumberOfLocations = addslashes ($_POST['NumberOfLocations']);
            $LocationSize = addslashes ($_POST['LocationSize']);
            //INTERACTION FIELDS
            $QualityOfLead = addslashes ($_POST['QualityOfLead']);
            $Notes = addslashes ($_POST['Notes']);
            $PropertyID = addslashes ($_POST['PropertyID']);
        }
        else
        {
            //CONTACT FIELDS
            $contactFirstName = $_POST['contactFirstName'];
            $contactLastName = $_POST['contactLastName'];
            $ContactTitle = $_POST['ContactTitle'];
            $contactPhoneNumber = $_POST['contactPhoneNumber'];
            $contactEmail = $_POST['contactEmail'];
            $InitialContactMethod = $_POST['InitialContactMethod'];
            //RETAILER FIELDS
            $DBA = $_POST['DBA'];
            $BusinessUse = $_POST['BusinessUse'];
            $StreetAddress = $_POST['StreetAddress'];
            $City = $_POST['City'];
            $State = $_POST['State'];
            $Zip = $_POST['Zip'];
            $NumberOfLocations = $_POST['NumberOfLocations'];
            //INTERACTION FIELDS
            $QualityOfLead = $_POST['QualityOfLead'];
            $Notes = $_POST['Notes'];
            $PropertyID = $_POST['PropertyID'];
        }

        //Begin Contact Insert
            mysqli_query(
                $con,"INSERT INTO Contact (ContactFirstName, ContactLastName, ContactTitle, ContactPhoneNumber, contactEmail, InitialMethodOfContact, CreatedDate) 
                VALUES ('" . $contactFirstName . "','$contactLastName', '$ContactTitle', '$contactPhoneNumber', '$contactEmail', '$InitialContactMethod', NOW())");

            $LastID = mysqli_insert_id($con);

        //Begin Retailer Insert
            $ContactIDValue = mysqli_insert_id($con);//Get the PrimaryKey which was generated in the above ContactInsert Statement

            mysqli_query(
                $con,"INSERT INTO Retailer (RetailerName, BusinessUse,  StreetAddress, City, State, ZipCode, NumberOfLocations, CreatedDate, ContactID) 
                VALUES ('$DBA', '$BusinessUse', '$StreetAddress', '$City', '$State', '$Zip', '$NumberOfLocations', NOW()," . $ContactIDValue . ")");

        //Begin Interaction Insert
            $RetailerIDValue = mysqli_insert_id($con);//Get the PrimaryKey which was generated in the above ContactInsert Statement

            mysqli_query(
                $con,"INSERT INTO Interaction (QualityOfLead, PropertyID, CreatedDate, Notes, ContactID, RetailerID) 
                VALUES ('$QualityOfLead', '$PropertyID', NOW(), '$Notes'," . $ContactIDValue . ", " . $RetailerIDValue . ")");

            mysqli_close($con);
    }
        ?>
                <a href = "http://leasinglog.com/" target="_blank"><input name="add" type="submit" id="add" value="Finish" class="button"></a>
                <h4 style="text-align:center; font-size: 12px; font-weight:200;"><a href="dashboard.php" action="_toBlank"><u><i>Cancel</i></u></a></h4>
            </form>
            <br>
            <hr style="width:400px; height:10px;">
        </div>   

<?php $_PHP_SELF ?> 的意思是当前文件,即包含您的表单的文件在表单提交时被调用。因此,您可以保持不变,然后在页面顶部添加重定向代码,并在提交表单时触发它。请注意,必须在任何其他输出之前调用 header 重定向 - 例如 html。

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
 //add any other checks such as form input values
 header('Location: http://myhost.com/mypage.php');
 exit;
}

或将您的表单操作更改为任何其他。php 您想要加载并处理那里的表单输入。

<form method="post" action="some_other.php" name="interactionForm">

更新:

header() 需要在任何输出之前进行,因此:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
     //add any other checks such as form input values
     header('Location: http://myhost.com/mypage.php');
     exit;
    }
    ?>
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta charset="utf-8" />
<title>New Interaction</title> <!--Declare CSS and JavaScript-->
<link rel="stylesheet" type="text/css" href="RealtyCRM_Style.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.resmenu.min.js"></script>
</head>

您还可以更新 php 设置以查看错误。这将具体告诉您您是否在重定向之前输出数据 - 除其他事项外,但这将是您的第一个警告。只需确保在完成后删除 ini_set()

<?php
    ini_set('display_errors', '1');
    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
         //add any other checks such as form input values
         header('Location: http://myhost.com/mypage.php');
     exit;
   }
  ?>