简体   繁体   中英

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. so php file, 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.

$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. And fetch the data in php file using $_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.

.ajax looks after all that.

pass

data: {newhours: newhour},

then you will get $_POST['newhours'] in the PHP code which will be the array you had in javascript.


In Ajax there is not "type" params, use "method". Beside that everything should works, you might need to decode the json using json_decode($_POST['data']) on in your php file.
Hopes it helps !

  • Nic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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