dongming0505 2016-02-20 15:09
浏览 58
已采纳

本地存储javascript变量到php

Thanks for your time in reading this post.

I am facing issues while passing javascript variable to php file.

Please look at the below code for your reference which i have written in checkout.php.

    $.ajax({
type: "POST",
url: "checkout.php",
data: localStorage.getItem("simpleCart_items"),
success: function(data) {
console.log("result"+JSON.stringify(data))

My php code :

<?php $data=$_POST['data']; echo $data;?>

Redirect is happening but $data value is null.

  • 写回答

1条回答 默认 最新

  • dqjo4340 2016-02-20 15:25
    关注

    The { data: data } is passed only to the first checkout.php call using POST method. When you redirect the user with window.location.href it has nothing to do with the previous call.

    Depending on your use-case you could in the first call to checkout.php save data to session and the access it in the second call but it really depends what you're trying to do.

    Edit: jQuery's data parameter is the entire payload you want to send to server. So if you were sending:

    $.post({
        data: {
            message: 'Hello'
        }
        ...
    });
    

    It'll be available in checkout.php as $_POST['message'] and not $_POST['data']['message'].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制