Note that there are some explanatory texts on larger screens.

plurals
  1. POsave jquery post data in a variable
    primarykey
    data
    text
    <p>I have read so many questions about this issue and I can't find an answer.</p> <p>All I want to do (which I thought would be the simplest thing ever in 2011) is get some info from my database with jquery and ajax and save it in a variable to later do some stuff with it. 5 hours later I'm still here. Is this impossible to do?</p> <p>The typical examples for this are:</p> <pre><code>$.post("test.php", { name: "John", time: "2pm" }, function(data) { alert("Data Loaded: " + data); }); </code></pre> <p>and </p> <pre><code>$.post("test.php", { name: "John", time: "2pm" }, function(data) { $("somediv").html(data); }); </code></pre> <p>So that works great... if you'll just use the value now and forget about it.</p> <p>Now I don't want to display the information on the page or in an alert box, I just want to save it in some variable so I can do some calculations later.</p> <p>when I assign data to variables, the values are always lost. After reading a bunch of answers about this, apparently there's no way to return data because this function is async and works differently and we don't understand how it works, etc.</p> <p>For example I tried this:</p> <p><a href="https://stackoverflow.com/questions/3080003/return-data-from-jquery-post">return data from jquery $.post</a></p> <p>But that didn't work for me. Or I didn't understand it correctly, is this an explanation or an example? Am I suppose to type callback or should I replace it with something?</p> <p>Some people said call another function like here:</p> <p><a href="https://stackoverflow.com/questions/3556080/jquery-return-post-data">Jquery return post data</a></p> <p>That didn't work either, data was passed to the function, i could use it there but again if I saved it in a variable it got lost and there was no way to return the function since where do I get what the function returns?</p> <p>Finally I said you know what, I'll just put the value in a hidden field and then I'll access it. That didn't work either, I can put the value in the field but when I try to access it... it's not there yet.</p> <p>Should I not use $.post? Does $.ajax make a difference? How can I accomplish this? Is it really so hard to ajax-get a value from the database and save it in javascript for later? Does everybody just use it right away? Is this really this hard to achieve?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload