duanqiao1880 2015-05-18 15:30
浏览 146

从localstorage到POST到PHP变量

I am executing a somewhat complex code from my point of view. It goes like this: First I am getting a list of numbers from localstorage from another page.

<script> 
var data = JSON.parse(localStorage.getItem('key'));
var localData = data.join(", ");  

Then I am posting data to PHP code on the same page like this:

$.ajax({
type: 'post',
       data: {localData: localData}, 
       dataType: "json",
       success: function(result){
       console.log(result)
       }  
       });
</script>

And then I want to put the data into a PHP variable and use it like this:

<?php
$user_id = isset($_POST['localData'])?$_POST['localData']:"";
$values = $user_id;
echo $values; ?>

The network inspector reveals the data is posted like this: localData 3, 5, 6, 8, 9

But I can't seem to get the data into the PHP variable and echo it. Please help.

  • 写回答

1条回答 默认 最新

  • douao1579 2015-05-18 15:39
    关注

    Your initial php tag is wrong, change <php? to <?php, also, in order to convert json to an array, you should use json_decode(), i.e.:

    <php?
    $user_id = isset($_POST['localData'])?$_POST['localData']:"";
    $values = json_decode($user_id);
    var_dump($values);
    

    Tip:

    Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code.
    Remember to comment or remove it in production mode.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog