Question

$.post('testeUpdate.php', 'autocomplete',
        function(dadosResposta) {

            $('#ns-nome').val(dadosResposta.nsName);
            $('#ns-endereco').val(dadosResposta.nsAddress);
        },
    "json");

I'm trying to understand this. So, and having the jquery $.post reference link near me:

1) A post request is send to testeUpdate.php, then, we can pass a string called 'autocomplete' . Precise?

Question 1) Passing a post request with that string, means that we can later, for example, process that request upon a conditional by specifically point to: $_POST['autocomplete']; ?

2) Later, we have a "on success" callback function what accepts a parameter, dadosReposta. Precise?

Question 2) This dadosResposta is something that may come from our server side script? Is this an argument having the data we receive?

3) So, on success, what we want to do is: populate some input element with some values. val(dadosResposta.nsName);

Question(s) 3) is this "dot notation" a way to access data on json format - or is this a way to walk on the DOM as we normally do? Is so, then what role is json playing here ?

Thanks a lot in advance, MEM

Was it helpful?

Solution

1) Yes then autocomplete can be accessed using PHP this way. ($_POST['autocomplete']). But other than the fact that it will show true on isset() - it won't have any data.

2) Yes dadosResposta is the response that will come back from the server. If you have set the last parameter as json on your $.post request, it can be used natively in javaScript as a json object. To display data in this way, in PHP you can use json_encode()

3) You can do as you please once your data comes in. But the dot notation will only work if the json is formatted properly. Ref : json.org

4) Dot notation is a way to access the data returned in the json format, has nothing to do with the DOM. If you change the last portion of your $.post to "text" the data returned from the server can be worked upon as a regular string.

OTHER TIPS

  1. Yes this is correct, it's the only post variable, e.g. you're checking isset($_POST['autocomplete']), though it seems you'd want to actually pass the textbox value here, since autocomplete normally relies on what you've typed already.
  2. Yes this function runs when the response comes back, the argument is whatever data comes back from your PHP page.
  3. The response is supposed to look something like this:
    { nsName: 'name', nsAddress: 'address' }
    It's using these values to populate those 2 fields, so the dot notation is getting the values from the response, JSON is just making this much cleaner :)

I used your code on jsFiddle and tried to built on what you have, as you can see I sent data to the json echo function on jsFiddle, which responded like this (probably... if its PHP):

echo json_encode(array("post_response"=>$_POST));

As you can see I changed the postData to match the response you were requesting, on the server side it would usually look a bit like this in a normal scenario:

if(isset($_POST['somedata']))
{
    //do stuff... you know, whatever
    echo json_encode(array("nsName" => $someString1,"nsAddress" => $someString2));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top