简体   繁体   English

将数组值从JavaScript传递到php

[英]pass array values from javascript to php

<script>
$("#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 jsonString = JSON.stringify(newhour);
   $.ajax({
         type: "POST",
         url: "exp.php",
         data:{data: jsonString}, 
         cache: false,
         success: function(){
                  alert("OK");
         }
   });
});
<script>

I want to pass the newhour array values to php and use them to insert into a table. 我想将newhour数组值传递给php,并使用它们插入表中。 so php file, exp.php: 因此php文件,exp.php:

$data = explode(",", $_POST['data']);
foreach($data as $d){
  echo $d;
  $sql = "insert into exp_table (hour) values ('$d')";
  mysql_query($sql);
}

However I can not take the values. 但是我不能接受这些值。 What is wrong with the code? 代码有什么问题? Could you please help me? 请你帮助我好吗? Thanks in advance. 提前致谢。

according to the answers i tried this on php side, but it returns NULL. 根据我在php端尝试过的答案,但是它返回NULL。

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

Pass the array without JSON.stringify in ajax data post. 在ajax数据发布中传递不带JSON.stringify的数组。 And fetch the data in php file using $_POST['data'] . 然后使用$_POST['data']获取php文件中$_POST['data']

I just have tried below code and it working great. 我只是尝试下面的代码,它的工作很棒。

<?php
if(isset($_POST['data'])){
  print_r($_POST);exit;
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
   var newhour= [];
   arrayNumbers = [['07:00','08:30'],['08:30','18:00','19:00']];
   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

   $.ajax({
         type: "POST",
         url: "abc.php",
         data:{data: newhour}, 
         cache: false,
         success: function(data){
            console.log(data);
         }
   });
});
</script>

You do not have to stringify an array in the javascript. 您不必在javascript中对数组进行字符串化。

.ajax looks after all that. .ajax负责所有这些工作。

pass 通过

data: {newhours: newhour},

then you will get $_POST['newhours'] in the PHP code which will be the array you had in javascript. 那么您将在PHP代码中获得$_POST['newhours'] ,这将是您在javascript中拥有的数组。


In Ajax there is not "type" params, use "method". 在Ajax中没有“类型”参数,请使用“方法”。 Beside that everything should works, you might need to decode the json using json_decode($_POST['data']) on in your php file. 除了一切正常之外,您可能还需要在php文件中使用json_decode($_POST['data'])解码json。
Hopes it helps ! 希望能有所帮助!

  • Nic 尼克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM