简体   繁体   English

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

[英]how to redirect to another page in php an mySql Database?

how can I redirect the user to another php page based on the button he is pressing? 如何根据用户所按的按钮将用户重定向到另一个php页面? For example .. I want that once loaded the page, in it are generated buttons containing the "id" of the table taken from a database ... At the click of the button you are redirected to a page in which there are textbox with the fields belonging to the table id .. 例如..我希望加载页面后,在其中生成包含从数据库获取的表的“ 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(); ?> 

So I have to pick up the "idCantiere" and I have to make sure that by clicking on the button on the page that opens me there are textBox with the data of the table of the "idCantiere" 因此,我必须选择“ idCantiere”,并确保通过单击打开我的页面上的按钮来确保存在带有“ idCantiere”表数据的文本框

Very quickly written and not tested - you could perhaps do like this. 非常快速地编写且未经测试-您也许可以这样做。 The function has to be javascript to interact with the client's actions ( ie: button press ) but the processing of the task is done by php. 该功能必须是javascript才能与客户的操作(即按钮按下)进行交互,但是任务的处理由php完成。 So, in the loop pass the ID of the record to the function as an inline argument and reload the same page with a new querystring. 因此,在循环中,将记录的ID作为内联参数传递给函数,然后使用新的querystring重新加载同一页。 PHP will process the querystring, find the ID and then do a different database lookup to find the page that you want to redirect to. PHP将处理查询字符串,找到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>

I think you are confusing the static html between dynamic server page. 我认为您在动态服务器页面之间混淆了静态html。
1.PHP is responsible for fecthing data from database or server file system ,and send html tags to front end 1.PHP负责从数据库或服务器文件系统中感染数据,并将html标签发送到前端
2.the browser receives strings from php , and parse the strings to html elements ,finally starts to run javascript 2.浏览器从php接收字符串,并将字符串解析为html元素,最后开始运行javascript

If you want to redirect page. 如果要重定向页面。
In php header('Location: /your/path') 在php header('Location: /your/path')
In javascript , window.location.href='/your/path' 在javascript中, window.location.href='/your/path'

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

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