dshu1235 2015-01-30 03:52
浏览 38
已采纳

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

I m trying to post the value from my java_post.js into php_post.php and then retrieve in another javascript page, index.html. So far i can post the value into the php_post.php and retrieve back into my java_post.js as alert(data) but i cannot retrieve from my 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");
}

The value is retrieve when the client click the selected table and then post into the php_post.php. The php_post.php will filter the result and return to index.html.

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

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

echo $filtered_students;

So now i m able to retrieve the value and post into as an alert for my java_post.js but the value is no pass into index.html and i receive the error for undefined postvalue.

<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>

So my problem is now, is there any method that allow me to show the value in index.html from php_post.php. As a reminder the alert(data) from java_post.js is just a testing purpose to show the value did post and return from php_post.php

  • 写回答

2条回答 默认 最新

  • douyan9417 2015-01-30 04:13
    关注

    The issue you're having is that when you pass the data into your PHP file and receive the data back in your JavaScript, the information only lasts as long as your current request.

    To fix this issue, consider using PHP Session variables to store your data, so that you can retrieve it later.

    Example:

    // 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
    ?> 
    

    Now in another file (you would switch your HTML file to get from php_get.php):

    //php_get.php
    <?php
    
    start_session();          // Don't forget to start the session
    
    echo $_SESSION['filtered_students'];
    
    ?>
    

    More information here: http://www.w3schools.com/php/php_sessions.asp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集