douzhan1994 2018-05-24 05:27
浏览 2827


Hi i'm a beginner in using JavaScript i have this html page with JavaScript codes that receives data from the server and display it on this current page, what i'm trying to do is use that data and sending it to another PHP page for my SQL query to get back results.

    var json = sessionStorage.xhr;
    var object = JSON.parse(json);
    var hard =  object["red-fruits"];
    var string = JSON.stringify (hard);
    var stringData = encodeURIComponent(string);
        type: "POST",
        url: "http://localhost/web/main.php",
        data: {"dataA" : stringData}, 
        cache: false,
            success: function(){
    var user = sessionStorage.getItem('impData');

This is my PHP page codes, what i'm doing here is getting the data "dataA" from that html page and sending it to this PHP page for the SQL query and getting the results which is the "$haha" array and using JavaScript session function to send it back to the HTML page. But my console only shows "null" can anyone tell me if i'm doing anything wrong or have any suggestion would be really appreciated.


$connection = mysqli_connect("localhost","root","","") or 
die("Error " . mysqli_error($connection));
    if (isset($_POST['dataA'])) {
        echo $name = $_POST['dataA'];
    else {
        echo "Error";

$string = str_replace("]", "", str_replace("[", "", str_replace('"','',$falcon)));
$array = explode(',', $string);
$array2= implode("', '",$array);

$sql = // "SQL query"
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . 

while($row = mysqli_fetch_array($result)) {
    $haha[] =  $row['row_name'];


<script type="text/javascript">
    var tills = <?php echo '["' . implode('", "', $haha) . '"]' ?>;
    console.log (tills);
    sessionStorage.setItem('impData', tills);
  • 写回答

1条回答 默认 最新

  • douguanya4248 2018-05-24 05:54

    You are now mixing ajax and session data on a strange way. The session data used by your javascript will not be updated by the php-script till you refresh your page. The correct way to handle data is in the "success" function:

            type: "POST",
            url: "http://localhost/web/main.php",
            data: {"dataA" : stringData},
            dataType : "json",
            cache: false,
                success: function(data){

    and in you PHP output the data you want to send to the browser as a json string:

    echo json_encode($your_object);
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 基于RTKLIB框架写的精密单点定位-AR
  • ¥15 PFENet的预训练权重
  • ¥15 程序哪有错误怎么改?
  • ¥15 交换机和交换机之间的链路带宽以及主机带宽的理解
  • ¥15 ai创想家对战模式代码
  • ¥15 集合A由3个2行4列二维数组构成,从集合A中任意取一个二维数组元素、如果该二维数组元素的对应列位置的上、下两数都是奇数,而且仅有2个列是奇数/奇数,则该数组有意义,并放入集合B中打印输出。
  • ¥15 电信IPV6 无法外网访问吗
  • ¥15 有偿求效果比较好的遥感影像匹配的c++代码
  • ¥15 博主,你好,我下载了你的智能网联汽车辅助驾驶安全信息检测系统,现在不会运行,可以教我吗,
  • ¥15 怎么在excle输入下列公式