How to take multiple data from a page and display separately with AJAX

Asked

Viewed 371 times

0

I have a program where it creates cookies with the data that the user typed in the fields <input>. All this through AJAX. So far without problems, but in this file where I make the request, it returns me the data of the cookies created. What I want is to take this data and put it in two other fields <input>.

An example to clarify:

  • Two camps <input> name and email;

  • I send this data to another file that creates cookies with this data;

  • And two other fields I want to auto-fill with these cookies returned;

Only when returned, they come together, there is some array to take this information one at a time?

A detail, using pure Javascript, nothing jQuery.

  • With what pattern do you join, for example, the name with the email? For example: v_nome + ";" + v_email.

  • I’m sorry I didn’t understand your question

  • You said that the values of the fields, in your theoretical example, are saved in a cookie, but "when returned, they come together". In what way did you save them together? In other words, how did you serialize the data?

  • I simply print the cookie on the screen before returning. E.g.: echo $_COOKIE['name']; echo $_COOKIE['email'];

  • Ah, I get it, the data will stop in particular cookies :) But if you’ve already separated the wheat from the chaff, what’s the problem?

  • Yes, but when I use the responseText function, it returns me these two values with a single string, so you want to know how to first send the echo $_COOKIE['name']; and then the echo $_COOKIE['email'];

  • @Math, instead of putting that kind of detail in the comments, is better [Edit] the question to include them.

Show 2 more comments

1 answer

2


One of the solutions, and the one that I consider more reliable (in terms of possible entries that can generate confusion in the logic of the script), is to pre-format the answer still on the server side, so that the client side does not need to decode it.

If you can get PHP to respond to Ajax with something like this:

'<script>
    var resposta = {
        "nome": "' . $_COOKIE['nome'] . '",
        "email": "' . $_COOKIE['email'] . '"
    };
    fcPreencheCampos(resposta);
</script>'

That way, in your responseText, you will have the source code that must be fixed in a <div> (for example) to be promptly executed.

function ajax(){
    // ...

    // Ao receber a resposta, e após verificar se o status é 200, etc etc:
    document.getElementById("divExecuta").innerHTML = xhr.responseText;

    // ...
}

At this point, just have a function, declared in a script <head>, for example, send the data to the fields as soon as they arrive:

function fcPreencheCampos(dados){
    document.getElementById("inputNome").value = dados.nome;
    document.getElementById("inputEmail").value = dados.email;
}

The Jsfiddle it won’t help much in this case; I hope the explanation will be enough for you to understand! Hugs!

  • Yes, pre-format before sending the result prevents so much headache..!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.