我正在使用一个包含四个模板的主 PHP 页面:
<?php session_start(); ?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=9" />
<title>Example Page</title>
</head>
<body>
<?php include ("script/select.class.php"); ?>
<div class="content">
<?php
include("./templates/one.php");
include("./templates/two.php");
include("./templates/three.php");
include("./templates/four.php");
?>
</div>
</body>
</html>
'two.php' 包含一个通过 AJAX 提交的表单 (the_form):
$('#button').click(function(){
$.ajax({
type: "POST",
url: "script/add_new.php",
data: $("#the_form").serialize(),
success: function(data){
newID = data;
alert(newID);
}
});
});
在“add_new.php”上,一旦插入完成,我就有了这个 PHP 代码:
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
$_SESSION['new_id'] = mysqli_insert_id($con);
echo $_SESSION['new_id'];
我遇到的问题是,即使 $_SESSION['new_id'] 将通过 Ajax 'data' var 传回并显示在 'alert' 中,如果我尝试在下一次显示时出现未识别索引错误使用<?php echo $_SESSION['new_id'] ?>
.
有什么建议么?