所以我不知道如何在标题中正确表达它,但我要在这里进行。我有两个页面,在第一个页面中,我添加了带有某些项目的列表视图(每个都有值)。我想要的是,当用户单击某个项目时,他/她会转到第二页,并显示给他/她一个列表视图(该列表视图将根据他们选择的值检索MySQL数据)。但是我的问题是,当我单击第一页中的项目时,第二页中没有显示任何内容。

我已经用几种方法尝试了几天,现在变得越来越令人沮丧!我真的需要帮助。非常感激。

这是我的第一页代码:

<?php
session_start();
include "partials/connectDb.php";

if (isset($_POST["goverVal"]))
{
    $_SESSION["goverVal"] = $_POST["goverVal"];
}
?>

<div data-role="page" id="homepage">
    <!-- HEADER INCLUDE -->
    <?php include "partials/header.php"; ?>
    <div data-role="main" class="ui-content" align="center">
        <h2>Select A Governorate</h2>
            <form action="index.php" method="POST">
                <ul data-role="listview" id="goverVal" name="goverVal">
                <?php
                    include "partials/connectDb.php";

                    $sql = "SELECT * FROM governorate_table;";
                    $run_query = mysqli_query($conn, $sql);

                    while ($row = mysqli_fetch_array($run_query))
                    {
                        $gId = $row['g_id'];
                        echo "<li data-value='{$gId}'><a href='area_page.php' class='ui-btn'>$gId - $row[name]</a></li>";
                    }
                ?>
                </ul>
            </form>
    </div>
    <!-- FOOTER INCLUDE -->
    <?php include "partials/footer.php"; ?>
</div>


================================================== =====================

这是我第二页的代码:

<?php
session_start();
include "partials/connectDb.php";

$goverChoice = $_SESSION["goverVal"];
?>

<div data-role="page" id="areaPage">
    <!-- HEADER INCLUDE -->
    <?php include "partials/header.php"; ?>
    <div data-role="main" class="ui-content" align="center">
        <h2>Select an Area</h2>
        <?php
            include "partials/connectDb.php";

            $q = "SELECT * FROM area_table WHERE governorate = '$goverChoice';";

            $run_sql = mysqli_query($conn, $q);

            echo "<ul data-role='listview'>";
            while ($row = mysqli_fetch_array($run_sql))
            {
                echo "<li><a href='#'>".$row['area_name']."</a></li>";
            }
            echo "</ul>";
        ?>
    </div>
    <!-- FOOTER INCLUDE -->
    <?php include "partials/footer.php"; ?>
</div>

最佳答案

没关系。我终于找到了合适的解决方案。
如果有人遇到相同的问题,这就是我使用的方法:

在第一页中:

$(document).ready(function(){
    $('#goverVal').on("click", "li", function(){
       $.ajax("update_session.php",{method:"post", data:{val:$(this).attr("data-value")}});
    });
});


而这在一个单独的文件中:

<?php
//Another File
session_start();
if (isset($_POST["val"]))
{
    $_SESSION["val"] = $_POST["val"];
}
?>


第二页的该行顶部显示数据:

$goverChoice = $_SESSION["val"];

关于javascript - JQuery Mobile PHP MySQL从第一页基于第二页的选择中检索数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38933924/

10-13 03:46