1

Just starting to learn about ajax although I am running into trouble trying to return a success message in an array.

<script type="text/javascript">
$(function () {
    $('#delete').on('click', function () {
        var $form = $(this).closest('form');
        $.ajax({
            type: $form.attr('method'),
            url: $form.attr('action'),
            data: $form.serialize()
        }).done(function (response) {
            if (response.success) {
                alert('Saved!');
            } else {
                alert('Some error occurred.');
            }
        });
    });
});
</script>

<?php
$array = array();
$array['success'] = TRUE;
echo $array;
?>

response.success should refer to $array['success'] correct?

user756659
  • 3,372
  • 13
  • 55
  • 110

1 Answers1

5

You are trying to echo your array, which will just spit out "Array".

Instead you need to encode your array as JSON and echo that out.

Change this:

echo $array;

To this:

echo json_encode($array);

Also you probably need to add your dataType in your ajax params so jQuery auto-parses the response:

dataType : 'json' // stick this after "data: $form.serialize()" for example

Also, here's a good post to read on how to properly handle success/errors with your ajax calls (thanks @Shawn):

Jquery checking success of ajax post

Community
  • 1
  • 1
jszobody
  • 28,495
  • 6
  • 61
  • 72
  • I would also recommend that you check out http://stackoverflow.com/questions/555315/jquery-checking-success-of-ajax-post OR http://stackoverflow.com/questions/555315/jquery-checking-success-of-ajax-post – Shawn Dec 05 '13 at 21:29
  • @Shawn That answer recommends `success` and `error` which have been deprecated as of jQuery 1.8. – jszobody Dec 05 '13 at 21:31
  • 1
    @jszobody No, the success and error options are NOT deprecated. the success and error METHODS are deprecated. two totally different things. – Kevin B Dec 05 '13 at 21:34
  • @KevinB You're absolutely right, I misread that other answer. Will update. – jszobody Dec 05 '13 at 21:35
  • @user756659 Was this helpful? Is your issue resolved? – jszobody Dec 05 '13 at 22:14
  • Sorry, was eating... echo json_encode($array); is still returning the false alert. – user756659 Dec 05 '13 at 22:18
  • If you still have issues, do some basic debugging. Look at the network tab in dev tools to see what the server response is. Do an `alert(response)` or `console.log(response)` in your success handler to see what you got back. Let us know, we'll be happy to help if you have more details. – jszobody Dec 05 '13 at 22:21
  • Works as expected. Thank you... really appreciate it. Time to dig into this some more, but something as simple as this really helped. You wouldn't know of a good tutorial/walkthrough off hand would you? I will need to return and use some values so that is the next step in this process, but it appears I am correct in terms of the array format for returns. – user756659 Dec 05 '13 at 22:24
  • @user756659 You are absolutely correct in using arrays for your return (passing through json_encode of course). Look up some jQuery ajax tutorials, there are a boatload. Good luck! – jszobody Dec 05 '13 at 22:25