我有一个起始页面,用户提供一些信息(电子邮件和姓名)并按提交。输入第二页时,jquery会从该第二页内向该第二页发送一些变量( dateStr )。但是,我注意到这样做会使表单提交变量消失。我在这里展示了代码的相关部分。 YourEMail 和 YourName 此处为空字符串。我在做什么错误?
<?php
$YourEMail = $_REQUEST['YourEMail'];
$InputName = $_REQUEST['YourName'];
$dateAndTime = '';
if(isset($_POST["dateStr"]) && strlen(trim($_POST["dateStr"])) > 0)
{
$dateAndTime = $_POST["dateStr"];
// $dateAndTime is fine, but $YourEMail and $InputName are empty here!
......
}
?>
从第二页内发布$ dateStr的jquery代码如下所示。
<script type="text/javascript">
$(function(){
var d = new Date();
var dateStr = d.toLocaleString();
$.post(window.location, {
dateStr: dateStr
}).success(function(data){
$('#divMessage').replaceWith("<span style=\"color:yellow\">" + dateStr + "</span>");
});
});
</script>
</head>
答案 0 :(得分:0)
我刚刚更改了你的Jquery代码。试试吧: -
<script type="text/javascript">
$(function(){
var d = new Date();
var dateStr = d.toLocaleString();
var data ={ dateStr: d.toLocaleString(),YourEmail :'<?=$_POST['YourEMail']?>',YourName : '<?=$_POST['YourName']?>'}
$.post(window.location, data ).success(function(data){
$('#divMessage').replaceWith("<br /><span style=\"color:yellow\">dateStr = "
+ dateStr + "</span>");
});
});
</script>
</head>
答案 1 :(得分:0)
<?php
// Start the session
session_start();
?>
....
<?php
if (isset($_POST['submit']))
{
$YourEMail = $_REQUEST['YourEMail'];
$InputName = $_REQUEST['YourName'];
$_SESSION["email"] = $YourEMail;
$_SESSION["name"] = $InputName;
}
$dateAndTime = '';
if(isset($_POST["dateStr"]) && strlen(trim($_POST["dateStr"])) > 0)
{
$YourEMail = $_SESSION['email'];
$InpuName = $_SESSION['name'];
$dateAndTime = $_POST["dateStr"];
......
}
?>