简体   繁体   English

在页面之间立即传递php变量

[英]Immediate passing php variable between pages

I need to send php from other php page back to index at the same time. 我需要同时将其他php页面的php发送回索引。 I use $_SESSION , but it shows result only after I refresh page. 我使用$ _SESSION,但仅在刷新页面后显示结果。 In index I'm getting values from inputs sending them through button onClick action myAjax(). 在索引中,我从通过按钮onClick动作myAjax()发送给它们的输入中获取值。

function myAjax() {
var range = document.getElementById('range').value;
var port = document.getElementById('port').value;
$.ajax({
   type: "POST",
   url: 'nova2.php',
   data:{action:'call_this',range:range,port:port},
   success:function(html) {
    // alert(html);
   }
});
}

to nova2.php, where I do query on database and getting array which I want to send back to index.php 到nova2.php,在该数据库中查询数据库并获取要发送回index.php的数组

while ($row = $query2->fetch_assoc()) {
    $port_list[] = $row['port_name'];

}

$_SESSION["var_name"] = $port_list;
mysqli_close($con);

I put almost same ajax script under this php function (of course without session), but it wasn't working. 我在这个php函数下放置了几乎相同的ajax脚本(当然没有会话),但是它没有用。 I didn't recieve any value in index.php 我在index.php中没有收到任何值

type: "POST",
url: 'index.php',
data:{action:'action',port_list:port_list},

Is there any other way? 还有其他办法吗? Thank you 谢谢

If you need SESSION value on same page where from you have requested AJAX to nova2.php. 如果您在同一页面上需要SESSION值,那么您已经从该页面向nova2.php请求了AJAX。

in nova2.php nova2.php

while ($row = $query2->fetch_assoc()) {
    $port_list[] = $row['port_name'];

}

$_SESSION["var_name"] = $port_list;
mysqli_close($con);
echo json_encode(array('port_list'=>$port_list));

In your AJAX function 在您的AJAX函数中

function myAjax() {
var range = document.getElementById('range').value;
var port = document.getElementById('port').value;
$.ajax({
   type: "POST",
   url: 'nova2.php',
   dataType: 'json',
   async: false,
   data:{action:'call_this',range:range,port:port},
   success:function(data) {
     port=data.port_list;
   }
});
}

I hope it should be work. 我希望它应该起作用。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM