首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >会话变量即使在包含session_start()之后也不更新

问会话变量即使在包含session_start()之后也不更新
EN

Stack Overflow用户
提问于 2017-11-09 07:47:07
回答 1查看 757关注 0票数 1

我有一个名叫train_detail.php.的页面该页面上有一个使用SQL创建的表,并将train_id作为其列属性之一。在每一行的末尾都有一个名为check_availability.的按钮当单击此按钮时,将打开一个新的模式。它将把行value(train_id)传递给模态。我使用JS将值通过post方法传递给页面helpe.php。现在,我制作了这个$_SESSION变量,并将$_POST变量的值传递给会话variable.But。问题是会话变量无法更新其值。请帮帮我。

列车详细.train

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <title>Home</title>
    <link rel="stylesheet" type="text/css" href="style.css">
    <style>
    .header{
        width: 70%;
    }
    .content {
        width: 70%;
    }
    <?php  
    session_start();
    $db = mysqli_connect('localhost', 'root', '', 'registration');
    $Source =$_SESSION['Source'];
    $Destination = $_SESSION['Destination'];
    $query = "SELECT * FROM train_details WHERE train_source='$Source' AND train_destination='$Destination'";
    $results = mysqli_query($db, $query);
?>

</style>
<style>
table {
    font-family: arial, sans-serif;
    border-collapse: collapse;
    width: 100%;
}

td, th {
    border: 1px solid #dddddd;
    text-align: center;
    padding: 8px;
}

tr:nth-child(even) {
    background-color: #dddddd;
}
</style>
<style>
#myDIV {
    width: 100%;
    padding: 50px 0;
    text-align: center;
    background-color: lightblue;
    margin-top:20px;
}
.hidden{
       display:none;
    }
</style>
</head>
<body>
    <div class="header">
        <h2>Train details</h2>
    </div>

    <div class="content">


            <table>
        <tr>
            <th>Train no.</th>
            <th>Train name</th>
            <th>Train departure</th>
            <th>Train arrival</th>
            <th></th>

        </tr>
        <script>
        function myFunction() {

}
        </script>
        <tbody>
        <!--Use a while loop to make a table row for every DB row-->
            <?php while( $row = mysqli_fetch_array($results)) : ?>
        <tr>
            <!--Each table column is echoed in to a td cell-->
            <td><?php echo $row["train_id"]; ?></td>
            <td><?php echo $row["train_name"]; ?></a></td>
            <td><?php echo $row["train_departure"]; ?></td>
            <td><?php echo $row["train_arrival"]; ?></td>
            <td>
                <button type="button" id="availability"  class=" open-AddBookDialog btn btn-info btn-lg" data-toggle="modal" data-id="<?php echo $row["train_id"]; ?>"
                href="#addBookDialog">Check availability</button>
            </td>

        </tr>
        <?php endwhile ?>
        </tbody>
</table>    

        <!--modal-->
        <div class="modal fade" id="addBookDialog" role="dialog">
            <div class="modal-dialog">
                <!-- Modal content-->
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                        <h4 class="modal-title">Modal Header</h4>
                    </div>
                    <div class="modal-body">


                    <p><?php echo $_SESSION['var_train2']  ?></p>
                    <input type="text" name="bookId" id="bookId" value=""/>

                    <div id="train">
                    </div>
                    <table style="width: 100%">
                        <tr>
                            <th>Date</th>
                            <?php
                            $query = "SELECT * FROM train_status WHERE 1";
                            $results = mysqli_query($db, $query);
                            ?>
                            <?php while( $row = mysqli_fetch_array($results)) : ?>
                            <th><?php echo $row["train_date"]; ?></th>
                            <?php endwhile ?>       
                        </tr>
                        <tr>
                            <th>availability</th>
                            <?php
                            $query = "SELECT * FROM train_status WHERE 1";
                            $results = mysqli_query($db, $query);
                            ?>
                            <?php while( $row = mysqli_fetch_array($results)) : ?>
                            <th><?php echo $row["availability"]; ?><br>
                            <a href="login.php?logout='1'" style="color: blue;">book now</a></th>
                            <?php endwhile ?>
                        </tr>

                    </table>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" onclick="myFunction()" data-dismiss="modal">Close</button>
                        <script>
                        function myFunction() {
                        location.reload();

                        }
                        </script>
                    </div>
                </div>
            </div>
        </div>

        <script type="text/javascript">
            $(document).on("click", ".open-AddBookDialog", function () {
            var myBookId = $(this).data('id');
            $(".modal-body #bookId").val( myBookId );

            $.ajax({
            type: 'post', // the method (could be GET btw)
            url: 'helper.php', // The file where my php code is
            data: {
            'test': myBookId // all variables i want to pass. In this case, only one.
            },
            success: function(data) { // in case of success get the output, i named data
            alert(data); // do something with the output, like an alert
            }
            });
            }); 

        </script>   

            <p> <a href="login.php?logout='1'" style="color: red;">logout</a> </p>


    </div>

</body>
</html>

helper.php-

代码语言:javascript
复制
<?php
    session_start();
    if(isset($_POST['test'])) { //if i have this post

    echo $_POST['test']+1; // print it   
    $_SESSION["var_train2"] =$_POST['test'];
    echo $_SESSION['var_train2'];

}
?>
EN

回答 1

Stack Overflow用户

发布于 2017-11-09 08:31:39

您正在混合两个编程层:

  1. PHP层在服务器上进行处理,然后再将任何内容传递给客户端。
  2. JS层是在客户端上进行处理的,是在服务器完成处理并将内容交付给客户端之后进行的。

因此,client (JS)根本无法读取任何服务器变量的内容,包括´$_SESSION´var。模式对话框通过JS (隐藏在BS代码的深处)显示为引导(BS)框架的一部分。它是通过纯粹使用客户端JS代码显示的,没有额外的服务器往返。因此,这一行中的PHP:

代码语言:javascript
复制
<p><?php echo $_SESSION['var_train2']  ?></p>

是处理服务器端,方式在之前,用户单击任何按钮,甚至在客户端收到任何之前。因此,假设会话一开始为空,呈现的HTML代码如下所示:

代码语言:javascript
复制
<p></p>

签入浏览器的本地HTML显示(查看源命令)以进行验证。这就是为什么,当您单击按钮时,您在对话框中看不到任何值。

因此,现在,当您的客户端按钮被单击时,您可以去调用AJAX调用中的助手脚本。这将设置会话var中按钮的ID。发生了什么:

  1. 按下按钮会触发客户端 JS脚本,不能设置任何$_SERVER var。
  2. JS脚本运行AJAX调用,请求服务器端的助手脚本。
  3. 服务器端的助手脚本在服务器上执行,使用AJAX调用提交的内容设置$_SESSION var。
  4. 服务器端助手脚本终止,$_SESSION var现在包含新值。
  5. 客户端引导部分显示仍然包含原始空行的模态对话框:

<p></p>

现在你得到了交易的一部分,你设置了$_SERVER变量。但是,如果没有重新加载,则不会显示客户端。要在客户端上显示代码,您有多种可能:

  1. 将模式对话框放在单独的php文件(dialog.php)中,并在浏览器弹出窗口或浏览器主窗口中打开该文件,替换当前内容。将火车ID发送到该dialog.php,并从数据库中读取所需的所有内容。这里不需要任何ajax,也不需要任何BS模式对话框。
  2. 使用当前的BS模式对话框,但使用JS填充它的内容。

我不打算详述第一种情况,只谈第二种情况:

更改上面的行以包含ID:

<p id="myID"></p>

在onclick函数中,除了现有代码之外,还可以使用JQuery设置<p id="myID"></p>标记的内容:

代码语言:javascript
复制
$(".modal-body #bookId").val( myBookId );
$("#myID").text(myBookId); // new

现在,对话框应该显示“按”按钮的ID。如果希望通过AJAX从服务器加载更多数据并显示在对话框中,则可以考虑使用像AngularJS这样的框架来为您工作。当然,那里的学习曲线更陡峭。

另一个(完全不同的)问题可能是构建查询的行中可能出现SQL注入错误:

代码语言:javascript
复制
$query = "SELECT * FROM train_details WHERE train_source='$Source' AND train_destination='$Destination'";

这看起来很容易受到SQL注入错误的影响。例如,如果有人将源设置为恶意源,则可以完全更改查询的含义。您应该使用字符串封装您的值,或者首先使用准备陈述。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47196513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档