0

So I'm learning Ajax and I followed this tutorial: https://phpacademy.org/course/submitting-a-form-with-ajax to create a function that works for any form. This is the complete function:

$('form.ajax').on('submit', function() {
    var that = $(this),
        url = that.attr('action'),
        type = that.attr('method'),
        data = {};

    that.find('[name]').each(function(index, value) {
        var that = $(this),
            name = that.attr('name'),
            value = that.val();

        data[name] = value;
    });

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response) {
            alert(url + ' ' + response);
        }
    })
    return false;
});

As you see all I'm doing is alerting the response, which in this case is php echo. That's where my problem starts, I need a flexible and secure way to handle the javascript/php communication. Preferrably one function for all forms, maybe with if () statements instead of alert(url + ' ' + response); depending on the form url.

The function I need is not very complicated, based on the response I get I will just update the current form with errors.

Aslong as I know what to use in the communication I'm fine from there, echo '1'; as I have now doesn't feel like the ideal solution but it's all I could handle.

So, what type of response should I send from the php page, and where (in the javascript) and how is the best way to handle it? I've heard of json but having a hard time wrapping my head around it. Could someone show a json response sending a basic integer thats easy for my response to pick up as an int?

4
  • Simple JSON response { response : 1 } - Accessed as response.response (I typically use data as my param name in the success callback, but that's all preference) Commented Sep 23, 2013 at 15:32
  • I tried getting it to work but I only get undefined back. In the php I did if () { response : 1} tried with semi-colon and everything I could think of, and in the js I do success: function(response) { alert(url + ' ' + response.response); } Commented Sep 23, 2013 at 15:43
  • You'll have to json_encode your data on the PHP side. (Assuming your type is json. Commented Sep 23, 2013 at 15:44
  • Guess I have to watch a simple json tutorial, change your comment to an answer and I'll give you credit. Commented Sep 23, 2013 at 15:50

1 Answer 1

2

You can send back a simple JSON response via PHP and check it in your success callback. Make sure you're setting the type to json and in your AJAX call, and using json_encode on the PHP side:

$response = array('response' => 1);
echo json_encode($response);

Output should be:

{ 'response' : 1 }
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget, header('Content-type: application/json');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.