简体   繁体   English

PHP 表单提交和页面重定向

[英]Php form submit and page redirect

I am trying to redirect a to a new php page after the user has clicked on the submit button.在用户单击提交按钮后,我试图将 a 重定向到新的 php 页面。 I have got it to successfully send the form information to the MySQL database but then I cannot get a successful redirect.我已经成功将表单信息发送到 MySQL 数据库,但是我无法成功重定向。

I then changed some code and got it to successfully redirect but not send the form information to the database.然后我更改了一些代码并使其成功重定向但未将表单信息发送到数据库。 My other php file is named nextForm.php and I have tried replacing the action="$_SERVER[PHP_SELF]" with the path to the nextForm.php file and I have tried using a require nextForm.php;我的另一个 php 文件名为 nextForm.php,我尝试将 action="$_SERVER[PHP_SELF]" 替换为 nextForm.php 文件的路径,并尝试使用 require nextForm.php; line in the code where I want to redirect.在我要重定向的代码行中。

Here is the code I have currently:这是我目前拥有的代码:

<?php
    
        //establish a connection to the MySQL db or terminate if ther is an error
        $conn = mysqli_connect("localhost","root","mysql","covid_tech",3306) or die(mysqli_connect_error());
        
        //HTML form to prompt user input
        print <<<_HTML_
        <FORM style="text-align:center" method="POST" action="$_SERVER[PHP_SELF]">
    
        <div class="Customer_Name">
        Enter Customer Name:  <input type="text" name="Customer_Name" class="textbox">
        </div>
        <br/>
        <div class="Contact_Name">
        Enter Contact Name:    <input type="text" name="Contact_Name" class="textbox">
        </div>
        <br/>
        <div class="Contact_Phone">
        Enter Contact Phone Number:   <input type="text" name="Contact_Number" class="textbox">
        <br/>
        </div>
        
        
        <button class="btn btn-1" style="text-align:center" onclick='disappear(this)' name="name_submit" method="POST" type="submit" value="find_cusName"><span>Enter Customer Name</span></button>
        </FORM>
        _HTML_;


        //check to make sure the POST request was sent and check to make sure that there is a vlaue in the System POST variable
        if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['Customer_Name'])){
            

            //SQL string to find the name that was input on the page
            $find_name_sql = "SELECT cusName,cusID from customer where cusName = '$_POST[Customer_Name]'";

            //run the query on the db
            $result_find_name = mysqli_query($conn, $find_name_sql);

            //Check to see if the query returned any rows
            if(mysqli_num_rows($result_find_name) > 0){
                
                //If it did, it should only be 1 row and we fetch it
                $row = mysqli_fetch_row($result_find_name);
                
                //set our current_id variable to the value in $row[1] which is the cusID attribute from the db
                $current_id = $row[1];
                
            }
            else{
                //sql statment to insert a new customer into the customer table of the db
                $insert_first_customer = "INSERT INTO customer (cusName,contactName,contactNo)  values('$_POST[Customer_Name]','$_POST[Contact_Name]','$_POST[Contact_Number]')";
                
                //run the insert query
                $add = mysqli_query($conn,$insert_first_customer);  
            }
            
            //redirect to next form page here   
        }
            
        
        mysqli_close($conn);
    ?>

The action attribute simply works as a way to direct your GET/POST requests. action属性只是作为引导 GET/POST 请求的一种方式。 If you would like to redirect after running your PHP code, you should use the header() function or use a meta tag.如果您想在运行 PHP 代码后重定向,您应该使用header()函数或使用meta标记。

Example:例子:

header('Location:'.$_SERVER['SERVER_NAME'].'/nextForm.php');

or或者

 echo '<meta http-equiv="refresh" content="0;url=nextForm.php">';

and finish your code with the exit() function so an attacker could not bypass your redirect.并使用exit()函数完成您的代码,这样攻击者就无法绕过您的重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM