如何重定向到mysql数据库中的另一个php页面?

uoifb46i  于 2021-06-19  发布在  Mysql
关注(0)|答案(2)|浏览(388)

如何根据用户按下的按钮将其重定向到另一个php页面?例如。。我想,一旦加载了网页,在它是生成的按钮,其中包含的“id”的表从数据库采取。。。单击按钮后,您将被重定向到一个页面,其中有一个文本框,其中的字段属于表id。。

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT idCantiere,nomeCantiere,codiceCommessa,indirizzoCantiere FROM Cantiere";
$result = $conn->query($sql);
echo'<h1> <font face="verdana" color="green">Quale Cantiere desideri Modificare?</font> </h1>';
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

		echo'<br><br><br>';
		echo'<a href="#" onClick=navigaButton(); class="myButton" alt="">' . $row["nomeCantiere"] . '</a>';
       // echo '<a href="#" class="rainbow-button" alt="' . $row["nomeCantiere"] . '"></a>';

		//''<input type="button" value="' . $row["nomeCantiere"] . '" />'
    }
	echo'<br><br><br>';
	echo '<a href="../pagineHtml/inserimento/inserimentoGenerale/inserimentoCantiere.php" class="myButton" alt="Nuovo Cantiere +">Nuovo Cantiere +</a>';

} else {
    echo "0 results";
}
$idCantierePerSelect = $_POST["idCantiere"];
global = $idCantierePerSelect;

function navigaButton()
{
  // FUNCTION That redirect to the page
};

$conn->close();
?>

因此,我必须拿起“idcantiere”,我必须确保通过点击打开我的页面上的按钮,有文本框与“idcantiere”表的数据

3mpgtkmj

3mpgtkmj1#

我认为你把静态html和动态服务器页面混淆了。
php负责从数据库或服务器文件系统中复制数据,并将html标记发送到前端
2.浏览器从php接收字符串,并将字符串解析为html元素,最后开始运行javascript
如果你想重定向页面。
在php中 header('Location: /your/path') 在javascript中, window.location.href='/your/path'

olqngx59

olqngx592#

写得很快,没有经过测试-你也许可以这样做。该函数必须是javascript才能与客户机的操作(即:按钮按下)交互,但任务的处理是由php完成的。因此,在循环中,将记录的id作为内联参数传递给函数,并用新的querystring重新加载同一页。php将处理querystring,找到id,然后执行不同的数据库查找以找到要重定向到的页面。

<?php

    $servername = "localhost";
    $username = "";
    $password = "";
    $dbname = "";

    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) die("Connection failed");

    /* 
        This section should be ignored when page loads normally
        but will be processed when the `navigaButton` is called
        and the url changes.
    */
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['action'],$_GET['id'] ) ){

        $sql='select NEWLOCATION from TABLE where ID=?';
        $stmt=$conn->prepare( $sql );
        if( $stmt ){
            $stmt->bind_param('i',$id);

            $id=filter_input( INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT );
            $stmt->execute();

            $stmt->store_result();
            $stmt->bind_result( $url );
            $stmt->close();

            /* read the recordset and .... */
            exit( header( 'Location: '.$url) );
        }

    }

?>

<!doctype html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>.... </title>
        <script>
            function navigaButton(id){
                location.search='action=redirect&id='+id
            };
        </script>
    </head>
    <body>
        <?php
            $sql = "SELECT idCantiere,nomeCantiere,codiceCommessa,indirizzoCantiere FROM Cantiere";
            $result = $conn->query($sql);

            echo'<h1> <font face="verdana" color="green">Quale Cantiere desideri Modificare?</font> </h1>';

            if ($result->num_rows > 0) {

                while($row = $result->fetch_assoc()) {

                    echo'
                    <br><br><br>
                    <a href="#" onClick=navigaButton('.$row['idCantiere'].'); class="myButton" alt="">' . $row["nomeCantiere"] . '</a>
                    <a href="#" class="rainbow-button" alt="' . $row["nomeCantiere"] . '"></a>';

                }

                echo'<br><br><br>
                <a href="../pagineHtml/inserimento/inserimentoGenerale/inserimentoCantiere.php" class="myButton" alt="Nuovo Cantiere +">Nuovo Cantiere +</a>';

            } else {
                echo "0 results";
            }

            $idCantierePerSelect = $_POST["idCantiere"];

            /* below is incorrect */
            /*global = $idCantierePerSelect;*/

            $conn->close();
        ?>
    </body>
</html>

相关问题