普通网友 2016-04-10 17:46
浏览 77
已采纳

表单提交后重定向用户

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.

The suggestions I have seen involve changing the Form Action however my code already has an Action of <?php $_PHP_SELF ?>. I am new to PHP so perhaps I can submit the form data in an alternative method than PHP_Self?

Code below, thank you in advance for the help and any suggestions on simplifying my code is also welcome

    <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>   
  • 写回答

1条回答 默认 最新

  • doujiang2641 2016-04-10 18:00
    关注

    What <?php $_PHP_SELF ?> means is that the current file, that is the file containt your form gets called on form submit. So, you can keep that the same and then add the redirect code at the top of the page and trigger it if the form is submitted. Note that a header redirect must be called before any other output - html for example.

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

    or change you form action to any other .php you want to load and handle the form inputs there.

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

    UPDATE:

    The header() needs to go before any output so:

    <?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>
    

    You can also update the php settings to view errors. This will tell you specifically if you're outputting data before the redirect - among other things but that would be among your first warnings. Just make sure to remove the ini_set() when you're done

    <?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;
       }
      ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮