[英]Pass variable from Javascript function to php in the same php file
[英]Failed to pass variable from a PHP function to another at the same file
我有兩個php函數getFeed ()
和selectURL ()
。 selectURL ()
函數旨在從javascript函數獲取URL作為String:
function selectURL () {
if(isset($_POST['Option']))
{
$uid = $_POST['Option'];
}
return $uid;
}
我想將此響應傳遞給getFeed()
函數。 換句話說,我想將此響應放在getFeed ()
函數中創建的$url
變量中:
function getFeed() {
// When I display $url
echo $url;
/** I want to get as response the link shown in the image (http://www.elkhabar.com/feeds)**/
}
單擊名為Start的Button時,必須執行getFeed()
:
<button type="submit" class="btn btn-default btn-success col-xs-3" id="start"> <i class="fa fa-play"></i>Start</button>
AJAX腳本:
$('#start').click(function(){
var $item = $('#test');
$.ajax({
type:'GET',
url: '/rss/core/inc/rssnews.inc.php?function=getFeed',
//dataType: 'json',
success: function (data){
var articles = $.parseJSON(data);
$.each (articles, function (key, value) {
$item.append('<div id="item"><ul><li><h4>'+ value.title +'</h4></li> <li>'+ value.description+'</li><li><a href="'+value.link+'">Lire+</a></li></ul>');
console.log('success',data);
});
}
});
});
那么,我怎么能這樣做?
您需要使用會話變量。 普通變量不會在不同腳本之間保留。
把session_start();
在腳本的開頭啟用會話。 然后將函數編碼為:
function selectURL() {
if (isset($_POST['Option'])) {
$_SESSION['url'] = $_POST['option'];
}
}
function getFeed() {
if (isset($_SESSION['url'])) {
$url = $_SESSION['url'];
} else {
$url = '';
}
echo json_encode($url);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.