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

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗