【发布时间】:2018-04-22 03:10:24
【问题描述】:
我有一个名为 train_detail.php 的页面。我在那个页面上有一个表,它是使用 SQL 制作的,并且有 train_id 作为它的列属性之一。在每一行的末尾都有一个名为 check_availability 的按钮。单击此按钮时,将打开一个新模式。它将行值(train_id)传递给模态。我使用 JS 通过 post 方法将值传递到页面 helpe.php 来做到这一点。现在,我已经创建了 $_SESSION 变量并将 $_POST 变量的值传递给了 SESSION 变量。但问题是会话变量无法更新其值。请帮帮我。
train_detail.php-
<!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'];
}
?>
【问题讨论】:
-
你在尝试做 $_SESSION["var_train2"] =$_POST['test'] +1; ?
-
echo $_SESSION['var_train2'] 给你输出了吗?
-
是的,它给了我一个输出。但是当我点击其他按钮时,这并没有改变。