簡體   English   中英

提交數據后嘗試重定向

[英]trying to redirect after submitting data

在將一些數據提交到example.php的數據庫中后,我嘗試進行重定向,我具有表單並且提交完美,但無法使重定向正常工作,我嘗試了幾種我在互聯網上看到的方法,但是沒有任何效果

<html>
    <head>

        <title>MySQLi Create Record</title>

    </head>
<body>

<?php
$action = isset($_POST['action']) ? $_POST['action'] : "";

if($action=='create'){
        //include database connection
        include 'db_connect.php';

        //write query
        $query = "insert into referb 
                                set
                                        make = '".$mysqli->real_escape_string($_POST['make'])."', 
                                        model = '".$mysqli->real_escape_string($_POST['model'])."',
                                        unitt  = '".$mysqli->real_escape_string($_POST['unitt'])."'";

        if( $mysqli->query($query) ) {
                echo " header( 'Location: example.php' ) ;";
        }else{
                echo "Database Error: Unable to create record.";
        }
        $mysqli->close();
}

?>

<!--we have our html form here where user information will be entered-->
<form action='#' method='post' border='0'>
    <table>
        <tr>
            <td>Manufactor</td>
            <td><input type='text' name='make' /></td>
        </tr>
        <tr>
            <td>Model</td>
            <td><input type='text' name='model' /></td>
        </tr>
        <tr>
            <td>Unit Type</td>
            <td>
            <select name='unitt'>
  <option>Laptop</option>
  <option>Computer</option>
  <option>Tablet</option>
</select></td>
        </tr>

            <td></td>
            <td>
                <input type='hidden' name='action' value='create' />
                <input type='submit' value='Save' />

                                <a href='index.php'>Back to index</a>
            </td>
        </tr>
    </table>
</form>

</body>
</html>

對header()的調用不應回顯。 該行應更改為:

header('Location: example.php');
exit();

之后調用exit()將停止腳本的進一步執行。

不要echo header("Location: example.php)". 這是一個php函數,因此您只需編寫

header("Location:example.php");

您不能在標頭之前或同一指令/條件中同時具有echo。

但是,您可以設置if指令。

您有echo " header( 'Location: example.php' ) ;";

它需要讀為header('Location: file.php'); 只要。

進行標頭重定向的正確方法是:

header('Location: example.php');

您可以執行以下操作:

if( $mysqli->query($query) ) {
    header('Location: example.php');
    }else{
            echo "Database Error: Unable to create record.";
        }
    $mysqli->close();
}

訪問在該PHP.net header()函數: http://php.net/manual/en/function.header.php

步驟1:首先執行查詢以將數據插入數據庫

步驟2:關閉數據庫連接

第3步:只需輸入>>>標頭(“位置:example.php”);

這是一個將用戶重定向到另一個URL的php腳本,將其放在正確的條件或您的代碼說明之后

 <html>
 <?php
 header('Location: http://www.example.com/');
 exit;
 ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM