douzhan1994 2018-05-24 05:27
浏览 2828
已采纳

将数据从前端发送到后端到前端

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.

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

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.

<?php

$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 " . 
mysqli_error($connection));

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);
</script>
  • 写回答

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:

    $.ajax({
            type: "POST",
            url: "http://localhost/web/main.php",
            data: {"dataA" : stringData},
            dataType : "json",
            cache: false,
                success: function(data){
                    console.log(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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵