tags:

views:

37

answers:

3
$.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

+1  A: 
  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 :)
Nick Craver
@all3) Ok, and that response can be build by using Kristoffer S Hansen example, like:echo json_encode(array("nsName" => $someString1,"nsAddress" => $someString2));}I'm still not connecting all the dots. Please have patience... :)I can "see" the server side: $dadosResposta = json_encode(array("nsName" => $someString1,"nsAddress" => $someString2));And the client side... function(dadosResposta)But how does $dadosResposta on the SS, connects with dadosResposta CS ?Thanks a lot,MEM
MEM
@MEM - The name `dadosResposta` has no meaning, you could just call it `data` on the client side...the server side also won't matter, it's just the root object, the response should look exactly like I have in the answer: `{ nsName: 'name', nsAddress: 'address' }`, that object is what the first parameter in the POST callback refers to. Also make sure to set the JSON MIME type header so the client (jQuery) knows it's JSON and to handle it that way, here's how: `header('Content-type: application/json');`
Nick Craver
@Nick Craver I realise the name doesn't matter.I believe the last argument on $.post (a string "json" would deal with jQuery way of reading the response as a json) yes?I believe I still don't have my answer there. I will make it on another question. :) Thanks a lot for time and comments. :)
MEM
+1  A: 

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.

DMin
Thanks a lot. So, about 1):Should I have any data passed there? I mean, I should pass the value of that input field, once the user stops typing, how can I do that? Will be the $.post argument the response to that?
MEM
to send the entire data of a particular form, the serialize() function in jquery is useful. You can do this : var aaa = $("#your_form_id").serialize(); $.post("your_url", aaa, function(){},"text_or_json"); -- so all your data will go to the php file. the name in your input will be the variable you can use to reference on the server. eg: the value for <input type="text" name="hello" /> inside your form can be accessed as $_POST['hello'] on the server.
DMin
you can study $.post in detail using jqapi.com its the same reference as the jquery website but its layed-out in a much better way and faster to navigate.
DMin
Ohhh! So that is just like a normal form. Ok... Almost there..The path from the client to the server, I'm getting, what I'm still not getting is the path from the server to the client... but as above, I will ask a new question concerning that.Thanks a lot
MEM
+1  A: 

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));
}
Kristoffer S Hansen
I was not aware of jsFiddle. I still don't know what to do with it... but I will give it a try. Thanks for the last snipped. With others answers I get a better understand about the all process involved.Thanks a lot. :)
MEM