我有一个名叫train_detail.php.的页面该页面上有一个使用SQL创建的表,并将train_id作为其列属性之一。在每一行的末尾都有一个名为check_availability.的按钮当单击此按钮时,将打开一个新的模式。它将把行value(train_id)传递给模态。我使用JS将值通过post方法传递给页面helpe.php。现在,我制作了这个$_SESSION变量,并将$_POST变量的值传递给会话variable.But。问题是会话变量无法更新其值。请帮帮我。
列车详细.train
<!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">×</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-
<?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'];
}
?>发布于 2017-11-09 08:31:39
您正在混合两个编程层:
因此,client (JS)根本无法读取任何服务器变量的内容,包括´$_SESSION´var。模式对话框通过JS (隐藏在BS代码的深处)显示为引导(BS)框架的一部分。它是通过纯粹使用客户端JS代码显示的,没有额外的服务器往返。因此,这一行中的PHP:
<p><?php echo $_SESSION['var_train2'] ?></p>是处理服务器端,方式在之前,用户单击任何按钮,甚至在客户端收到任何之前。因此,假设会话一开始为空,呈现的HTML代码如下所示:
<p></p>签入浏览器的本地HTML显示(查看源命令)以进行验证。这就是为什么,当您单击按钮时,您在对话框中看不到任何值。
因此,现在,当您的客户端按钮被单击时,您可以去调用AJAX调用中的助手脚本。这将设置会话var中按钮的ID。发生了什么:
$_SERVER var。$_SESSION var。$_SESSION var现在包含新值。<p></p>
现在你得到了交易的一部分,你设置了$_SERVER变量。但是,如果没有重新加载,则不会显示客户端。要在客户端上显示代码,您有多种可能:
我不打算详述第一种情况,只谈第二种情况:
更改上面的行以包含ID:
<p id="myID"></p>
在onclick函数中,除了现有代码之外,还可以使用JQuery设置<p id="myID"></p>标记的内容:
$(".modal-body #bookId").val( myBookId );
$("#myID").text(myBookId); // new现在,对话框应该显示“按”按钮的ID。如果希望通过AJAX从服务器加载更多数据并显示在对话框中,则可以考虑使用像AngularJS这样的框架来为您工作。当然,那里的学习曲线更陡峭。
另一个(完全不同的)问题可能是构建查询的行中可能出现SQL注入错误:
$query = "SELECT * FROM train_details WHERE train_source='$Source' AND train_destination='$Destination'";这看起来很容易受到SQL注入错误的影响。例如,如果有人将源设置为恶意源,则可以完全更改查询的含义。您应该使用字符串封装您的值,或者首先使用准备陈述。
https://stackoverflow.com/questions/47196513
复制相似问题