[英]Passing jQuery paramenter between pages
我正在尝试使用jQuery在PHP页面之间传递变量。
我的代码:我在fullcalendar.js API上使用了它:
第一页:EventMain.php
select: function (start, end) {
$.ajax({
url: '/dev/Event/Event.php',
type: 'POST',
data: {name: 'John'}
});
PopupCenter(url,'Event', 1000, 450)
}
然后弹出窗口显示第二个页面:Event.php。 我需要获取名称变量
var Name = <? php echo $_POST['name']; ?>;
alert(Name);
警报为空白。。我不明白为什么?
我知道我可以将URL上的变量作为参数发送,然后在Event.php上使用getUrlParameter来获取变量:
function getUrlParameter(sParam){
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
但是我不想在URL上发送变量。 还有其他发送变量的方法吗?
您无法在另一个会话中访问POST变量。
您打开的弹出窗口是对没有数据的页面的新请求。
您要实现的目标没有合理的解决方案。
将变量从EventMain.php
给Event.php
$.ajax({
type: 'post',
url : '/dev/Event/Event.php',
data: { name : 'John'},
success: function(response) { // return content from Event.php (result) page
// Do your operation with result
}
});
在Event.php中
<?php
$name = $_POST['name'];
// Perform your actions with $name and return result as
echo $yourresult;
exit;
?>
尝试这种方式...
eventMain.php
$.post('/dev/Event/Event.php',{'name':'John'}, function(data){
// do something with response
});
Event.php
<?php
$name = $_POST['name'];
?>
<script>
console.log('<?= $name; ?>');
</script>
我认为您在第二页中为变量name
分配值时忘记了""
。
第一页:
<!-- you don't forget this tag to see the result //-->
<div id="content"></div>
$.ajax({
url: 'page.php',
type: 'POST',
data: {name: 'John'},
success: function (data) {
$('#content').html(data);
}
});
第二页(event.php):
<script type="text/javascript">
var name = "<?php echo $_POST['name']; ?>";
alert(name);
</script>
之前的代码对我有用,我已经对其进行了测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.