将数组值从javascript代码传递到php。

时间:2022-10-16 12:39:11

I'm trying to pass the values of array to php but i can not get the values. It returns NULL, when I use var_dump() it returns array(1) { [0]=> string(0) "" } Array ( [0] => )

我试图将数组的值传递给php,但我不能得到值。它返回NULL,当我使用var_dump()时它返回数组(1){[0]=>字符串(0)"}数组([0]=>)

Here the code:

下面的代码:

$("#submit").click(function () {
   var newhour= [];
   for (var i = 0; i < arrayNumbers.length; i++) {
        newhour.push(arrayNumbers[i].toString().split(','));   
        console.log("each: " + newhour[i]); // output: each: 07:00,08:30
                                                       each: 18:00,19:00                                                   
   }
   console.log("all: " + newhour); //output: all: 07:00,08:30,18:00,19:00

   var jsonx = JSON.stringify(newhour);
   console.log("x: " +jsonx); // output: x: [["07:00","08:30"],["18:00","19:00"]]
   $.ajax({
         type: "POST",
         url: "hour.php",
         data:{data: jsonx}, 
         cache: false,
         success: function(){
                  alert("OK");
         }
   });
});

hour.php file include:

小时。php文件包括:

  $data = json_decode($_POST['data'],true);
//$data = json_decode(($_POST['data']));  
//$data = explode(",", $_POST['data']);
//$data = $_POST['data']; 
  echo "data: " .$data;
  var_dump($data);
  foreach($data as $d){
        echo $d;
  }

I struggled with that for hours, i tried everything but couldn't get any values. I don not understand where I'm wrong. Can you please help me with my code? Thanks in advance.

我挣扎了好几个小时,什么都试过了,但没有任何价值。我不明白我错在哪里。你能帮我写代码吗?提前谢谢。

1 个解决方案

#1


0  

It works fine for me This is the data I used for arrayNumbers

这对我来说很好,这是我用于arrayNumbers的数据。

var arrayNumbers = [1, 591, 283, 12];

var arrayNumbers = [1,591, 283,12];

This is the return data

这是返回数据。

data: Arrayarray(4) { [0]=> array(1) { [0]=> string(1) "1" } [1]=> array(1) { [0]=> string(3) "591" } [2]=> array(1) { [0]=> string(3) "283" } [3]=> array(1) { [0]=> string(2) "12" } } ArrayArrayArrayArray

数据:Arrayarray(4){[0]= >数组(1){[0]= >字符串(1)"1"}[1]=>数组(1){[0]=>字符串(3)"591"}[2]=>数组(1){[0]=>字符串(3)"283"}[3]=>数组(1){[0]=>字符串(2)} } ArrayArrayArrayArray“12”

EDIT: just tried a few different values for arrayNumbers. When i used var arrayNumbers = [""]; I got your exact return. Check what values you are using for that because thats the issue.

编辑:只是为arrayNumbers尝试了几个不同的值。当我使用var arrayNumbers = ["];我得到了你的确切回报。检查你所使用的值,因为这是问题所在。

Note: I also had to comment out each: 18:00,19:00, but I assumed you already had that commented and copied it wrong

注意:我也不得不注释掉每一个:18:00,19:00,但是我假设你已经有了这个评论并把它抄错了。

#1


0  

It works fine for me This is the data I used for arrayNumbers

这对我来说很好,这是我用于arrayNumbers的数据。

var arrayNumbers = [1, 591, 283, 12];

var arrayNumbers = [1,591, 283,12];

This is the return data

这是返回数据。

data: Arrayarray(4) { [0]=> array(1) { [0]=> string(1) "1" } [1]=> array(1) { [0]=> string(3) "591" } [2]=> array(1) { [0]=> string(3) "283" } [3]=> array(1) { [0]=> string(2) "12" } } ArrayArrayArrayArray

数据:Arrayarray(4){[0]= >数组(1){[0]= >字符串(1)"1"}[1]=>数组(1){[0]=>字符串(3)"591"}[2]=>数组(1){[0]=>字符串(3)"283"}[3]=>数组(1){[0]=>字符串(2)} } ArrayArrayArrayArray“12”

EDIT: just tried a few different values for arrayNumbers. When i used var arrayNumbers = [""]; I got your exact return. Check what values you are using for that because thats the issue.

编辑:只是为arrayNumbers尝试了几个不同的值。当我使用var arrayNumbers = ["];我得到了你的确切回报。检查你所使用的值,因为这是问题所在。

Note: I also had to comment out each: 18:00,19:00, but I assumed you already had that commented and copied it wrong

注意:我也不得不注释掉每一个:18:00,19:00,但是我假设你已经有了这个评论并把它抄错了。