将Javascript发布到PHP,然后在另一个javascript中检索

时间:2015-01-30 03:52:52

标签: javascript php ajax

我试图将java_post.js中的值发布到php_post.php,然后在另一个javascript页面index.html中检索。到目前为止,我可以将值发布到php_post.php并将java_post.js作为alert(data)检索回来  但我无法从我的index.html

中检索
Java_post.js

var url_link ="index.html";

//On Click Select Function
$("#table_hot").on('click', 'tbody tr',function(){
$(this).addClass('selected').siblings().removeClass('selected');
var value=$(this).find('td:first').html();

$.post('PHP_post/php_post.php',
    {
        postvalue:value
    },
    function(data){
    alert(data);
}
);
});

//Window Pop Out Function
function hotspot_pop(url_link){
newwindow = window.open(url_link, '', "status=yes, 
height=500; width=500; resizeable=no");
}

当客户端点击所选表格然后发布到php_post.php时,将检索该值。 php_post.php会过滤结果并返回index.html

$filtered_students = array_filter($ARRAY, function($row) {

$hotspot_value = $_POST['postvalue'];
if($row['name'] == $hotspot_value){
    return true;
}
});

echo $filtered_students;

所以现在我能够检索该值并将其作为我的java_post.js的警告发布,但该值未传递到index.html,并且我收到未定义postvalue的错误。< / p>

<html>
<script src="js/jquery-1.11.1.min.js"></script>
<body>
<div id="result"></div>
<script>
var xmlhttp_user = new XMLHttpRequest();
var url_user = "PHP_post/php_post.php";
xmlhttp_user.onreadystatechange=function() {
    if (xmlhttp_user.readyState == 4 && xmlhttp_user.status == 200) { 
      document.getElementById("result").innerHTML=xmlhttp_user.responseText;    }
}
xmlhttp_user.open("GET", url_user, true);
xmlhttp_user.send();
</script>
</body>
</html>

现在我的问题是,是否有任何方法可以让我在index.html php_post.php中显示价值。提醒一下,来自alert(data)的{​​{1}}只是一个测试目的,用于显示java_post.js

后发布和返回的值

2 个答案:

答案 0 :(得分:0)

您可以在php_post.php中将所需的值设置为PHP会话。 这样,您就可以在所需的任何页面上检索会话的值。

答案 1 :(得分:0)

您遇到的问题是,当您将数据传递到PHP文件并在JavaScript中接收数据时,信息只会持续您当前的请求。

要解决此问题,请考虑使用PHP会话变量来存储数据,以便稍后检索。

示例:

// php_post.php
<?php

start_session();   // initializes session for persistent data

$filtered_students = array_filter($ARRAY, function($row) {

$hotspot_value = $_POST['postvalue'];
     if($row['name'] == $hotspot_value){
         return true;
     }
});

$_SESSION["filtered_students"] = $filtered_students;   // You can now retrieve this in
                                                       // Another PHP file
?> 

现在在另一个文件中(您将切换HTML文件以从php_get.php获取):

//php_get.php
<?php

start_session();          // Don't forget to start the session

echo $_SESSION['filtered_students'];

?>

此处提供更多信息:http://www.w3schools.com/php/php_sessions.asp

相关问题