[英]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.