JQuery Mobile PHP MySQL从第一页的第二页选择中检索数据

时间:2021-06-11 17:00:15

so I didn't know how to properly express it in the title but I'm gonna do it here. I have two pages, in the first one I added a listview with some items (each got value). What I want is when the user clicks on an item, he/she goes to the second page and it'd show him/her a listview (that retrieves MySQL data based on the value they selected). But my problem is when I click on an item in the first page, it shows me NOTHING in the second page.

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

I have tried this with several methods for several days, and it's getting frustrating now! I really need help with it. Much Appreciated.

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

Here is my first page code:

这是我的第一页代码:

<?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>

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

And this is my second page's code:

这是我的第二页代码:

<?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>

1 个解决方案

#1


0  

Never mind. I just finally found an appropriate solution. Here is what I used if anyone ran into the same problem:

没关系。我终于找到了合适的解决方案。如果有人遇到同样的问题,我会使用以下内容:

In the first page:

在第一页:

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

And this in a separate file:

这是一个单独的文件:

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

And this line in the second page where to show the data, at the top:

并在第二页的这一行显示数据,在顶部:

$goverChoice = $_SESSION["val"];

#1


0  

Never mind. I just finally found an appropriate solution. Here is what I used if anyone ran into the same problem:

没关系。我终于找到了合适的解决方案。如果有人遇到同样的问题,我会使用以下内容:

In the first page:

在第一页:

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

And this in a separate file:

这是一个单独的文件:

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

And this line in the second page where to show the data, at the top:

并在第二页的这一行显示数据,在顶部:

$goverChoice = $_SESSION["val"];