j-man86 j-man86 - 2 months ago 6
PHP Question

Getting ajax post data on success

I have the following ajax call. What I would like to do is set the variable "lan_setting" during the ajax requests, and be able to use that variable on success.

In reality I want to set that variable to be post data, which will vary depending on the form input, but as of now I can't even get it working with just this basic example. It just returns "undefined".

_jqXHR = $.ajax({
url: url,
data: {lan_setting: 'en'},
scriptCharset: "UTF-8",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
success: function(lan_setting, data, textStatus, jqXHR) {
alert(data.lan_setting);
}
});


How do I can I use post variable sent via ajax on success?

thanks!

Answer

Well, if you're posting, you should use the jquery post function here

$.post(
    url,
    {lan_setting:"en"},
    function( data, status, jqXhr ){
        alert(data.lan_setting);
    },
    "json"
);

then php:

<?php
    // do stuff

    $response = new stdClass;
    $response->lan_setting = $_POST["lan_setting"];
    print json_encode($response);
?>
Comments