0
<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
}
Ozzlem
  • 29
  • 4
  • to convert `JSON` in php to array use `json_decode` not explode. – jcubic Sep 29 '16 at 12:30
  • Is the $_POST variable empty ? Do you get a hit on your exp.php file ? Do you have javascript error ? – Thibault Sep 29 '16 at 12:31
  • when I tried to get data with echo $data, it gives me that it is array. It is not give the values @Thibault – Ozzlem Sep 29 '16 at 12:34
  • Yes, explode() returns an Array. To display the content, use var_dump() or print_r() – Thibault Sep 29 '16 at 12:35
  • 1
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 29 '16 at 12:38
  • @Thibault, it returns NULL with var_dump($data); – Ozzlem Sep 29 '16 at 13:07

3 Answers3

0

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>
Rahul Patel
  • 5,248
  • 2
  • 14
  • 26
  • Oh yes, inserting `$_REQUEST` data into the database blindly seems like such a good idea... – Andrei Sep 29 '16 at 12:37
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Sep 29 '16 at 12:38
  • @RiggsFolly Dude, really, I gotta ask, are you parsing new questions and automatically posting that answer? Not complaining or anything, the sooner `mysql_*` dies the better. – Andrei Sep 29 '16 at 12:39
  • @Andrew No, :) I just have a set of comments sitting in an editor waiting to go. – RiggsFolly Sep 29 '16 at 12:40
  • @Ozzlem When you print_r($_POST) on the top of the PHP file what are you getting? – Rahul Patel Sep 29 '16 at 13:42
  • Pass the array from ajax without JSON.stringify and you will able to get the values by using $_POST itself no need to json_decode. – Rahul Patel Sep 29 '16 at 13:43
  • @RahulPatel , I did exactly what you suggest but still no data. Also I print_r($_POST) on the top of php code, it does not give the any array value. – Ozzlem Sep 29 '16 at 13:59
  • @Ozzlem can you please assign the newhour array like var newhour= {}; and try again. – Rahul Patel Sep 29 '16 at 14:05
  • @RahulPatel when I did this, it can not run my functions as newhour.push, it says it is not a function – Ozzlem Sep 29 '16 at 14:10
  • @Ozzlem Can you please check my answer now. Do you trying exactly same as my code? Please compare your arrayNumbers array as well. Because I have tried the exact code here and it is working great. – Rahul Patel Sep 29 '16 at 14:13
0

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.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
0


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
Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • `json_decode($var, true)` for array output. No `true` for `stdClass`. Directed towards OP. – Andrei Sep 29 '16 at 12:35
  • @Andrew Yea you're right, but in the end it gives the same results. – Nicolas Sep 29 '16 at 12:37
  • Absolutely, it wasn't criticism, I just figured OP may look over your answer and some people prefer working with arrays. It's nice to have that option too. No harm intended. – Andrei Sep 29 '16 at 12:38
  • "type" exists : http://api.jquery.com/jquery.ajax/ -> type (default: 'GET') An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0. – Thibault Sep 29 '16 at 12:38
  • @Thibault My bad, I never used type before.Both works i guess. – Nicolas Sep 29 '16 at 12:42
  • @Nicolas I learned it today too ;-) That's why I checked the doc :-) – Thibault Sep 29 '16 at 12:53