【问题标题】:how to redirect to another page in php an mySql Database?如何重定向到 php mySql 数据库中的另一个页面?
【发布时间】:2019-03-06 07:18:10
【问题描述】:

如何根据用户按下的按钮将用户重定向到另一个 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();
?>

所以我必须拿起“idCantire”,我必须确保通过单击打开我的页面上的按钮,有带有“idCantire”表格数据的文本框

【问题讨论】:

  • 函数navigaButton 需要是javascript,你需要以某种方式传递它的id。
  • @RamRaider 感谢您的回复,但我该如何传递呢?

标签: php html mysql web-applications


【解决方案1】:

写得很快,没有经过测试——你也许可以这样做。该函数必须是 javascript 才能与客户端的操作(即:按钮按下)进行交互,但任务的处理由 php 完成。因此,在循环中将记录的 ID 作为内联参数传递给函数,并使用新的查询字符串重新加载同一页面。 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>

【讨论】:

  • 再次感谢您的回答,但我不知道您是否理解我,我需要一种将变量“idCantiere”从一个页面传递到另一个页面的方法......假设这是页面“选择.php”,然后单击带有“idCantiere = 1”的按钮1,这个变量我想把它放在全局,这样它就可以从数据库的所有页面中查看“..在这种情况下,我打开一个通用页面“view.php”...我将在其中查询类型为“SELECT idCantire, nomeCantiere 等.. FROM Cantiere WHERE idCantire = 我从“choose.php”中获取的变量“
  • 这或多或少会发生(或应该)上面所做的更改 - 它会导致将查询字符串附加到您的页面 url - 类似于 http://www.example.com/index.php?id=23&amp;action=redirect - 其中 idCantiere 在这方面是 23,在查询字符串中称为id。如果这不是你的意思,那么我没有理解,也许你可以进一步解释?
【解决方案2】:

我认为您混淆了动态服务器页面之间的静态 html。
1.PHP负责从数据库或服务器文件系统中提取数据,并将html标签发送到前端
2.浏览器从php接收字符串,并将字符串解析为html元素,最后开始运行javascript

如果你想重定向页面。
在 php header('Location: /your/path')
在 javascript 中,window.location.href='/your/path'

【讨论】:

  • 这不是问题!也许我不必解释清楚..我需要一种方法在另一个 php 页面中传递像“idCantiere”这样的变量
  • @NarcosZTK_10 通过 PHP 生成 javascript,如 &lt;?php $id="xx";echo "&lt;script&gt;alert({$id})&lt;/script&gt;"?&gt; 。如果你想在每个 php 页面中使用 id,使用 $_SESSION 来存储它,如 a.php&lt;?php session_start(); $_SESSION['id']='xx';?&gt; b.php: &lt;?php session_start(); echo $_SESSION['id']; ?&gt;
猜你喜欢
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-19
  • 2021-10-01
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
相关资源
最近更新 更多