2

I want to pass an array through AJAX but I am not getting any feed back on what it is I am sending. I tried to do a var_dump($_POST); on the PHP side (next.php) but nothing is showing up. I'm guessing there is something wrong with my code.

function sendToServer(data) {
    $.ajax({
        type: "POST",
        data: { arr: JSON.stringify(data) },
        url: "next.php",
        success: function() {}
    });
}

next.php:

<?php
var_dump($_POST);
$data = json_decode(stripslashes($_POST['arr']));
foreach ($data as $item) {
   echo $item;
   // insert to db
}

?>

Full snippet of my code:

<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <style type="text/css">
    <!-- #main {
      max-width: 800px;
      margin: 0 auto;
    }
    -->
  </style>
</head>

<body>
  <div id="main">
    <h1>Add or Remove text boxes with jQuery</h1>
    <div class="my-form">
      <!--            <form action="next.php" method="post">-->
      <button onclick="addAuthor()">Add Author</button>
      <br>
      <br>
      <div id="addAuth"></div>
      <br>
      <br>
      <button onclick="submit()">Submit</button>
      <!--            </form>-->
    </div>

    <div id="result"></div>
  </div>

  <script type="text/javascript">
    var authors = 0;

    function addAuthor() {
      authors++;
      var str = '<br/>' + '<div id="auth' + authors + '">' + '<input type="text" name="author" id="author' + authors + '" placeholder="Author Name:"/>' + '<br/>' + '<button onclick="addMore(\'auth' + authors + '\')" >Add Book</button>' + '</div>';
      $("#addAuth").append(str);
    }

    var count = 0;

    function addMore(id) {
      count++;
      var str =
        '<div id="bookDiv' + count + '">' + '<input class="' + id + '" type="text" name="book' + id + '" placeholder="Book Name"/>' + '<span onclick="removeDiv(\'bookDiv' + count + '\')">Remove</span>' + '</div>';
      $("#" + id).append(str);
    }

    function removeDiv(id) {
      $("#" + id).slideUp(function() {
        $("#" + id).remove();
      });
    }

    function submit() {
      var arr = [];
      for (i = 1; i <= authors; i++) {
        var obj = {};
        obj.name = $("#author" + i).val();
        obj.books = [];
        $(".auth" + i).each(function() {
          var data = $(this).val();
          obj.books.push(data);
        });
        arr.push(obj);
      }
      sendToServer(arr);
      //$("#result").html(JSON.stringify(arr));
    }

    function sendToServer(data) {
      $.ajax({
        type: "POST",
        data: {
          arr: JSON.stringify(data)
        },
        url: "next.php",
        success: function() {

        }
      });

    }
  </script>
</body>

</html>
Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
learningbyexample
  • 1,527
  • 2
  • 21
  • 42
  • I think `json_decode` will give you out an object and I'm not sure if it's possible to use it on the `forEach`, try setting the second argument of `json_decode` to `true`. – MinusFour Dec 19 '15 at 20:25
  • see this http://stackoverflow.com/questions/14918462/get-response-from-php-file-using-ajax – Abhishek Singh Dec 19 '15 at 20:26
  • Where exactly do you expect the data to show up? Var dump prints to the response, which you don't receive in your `success` function. Take a look at Abhisheks suggestion, it's probably what you're doing wrong. – Pavlin Dec 19 '15 at 20:45
  • I added this: `success: function(data) { alert(data); } });` and the alert sends back the var_dump(); but the error I get is: Object of class stdClass could not be converted to string – learningbyexample Dec 19 '15 at 20:48
  • have you checked on the browser for a response from the server, do it by running on chrome ->inspect-> network-> xhr what do you get? – Damkulul Dec 19 '15 at 22:16

1 Answers1

0

The problem is when you try to echo the item. As $item is an object (stdClass), and the echo command expects a string, the echo command fails with "stdClass could not be converted to a string". You can either change to:

echo print_r($item, true);

or:

var_dump($item);

D G
  • 699
  • 7
  • 7