Send values to back Javascript with json and .post

All, I have the following code:

$.post("save.php", {rate: value, vendor: vendor_id}, function(results)
{
    alert(results);
}, "json");

I get the data and do some updates to the database. When I'm done I'd like to pass back $var1, $var2. Then parse those values and put them into a div. How can I send these values back so I can access them in something like:

results.var1

Then I can put that value of var1 into a div. Thanks for the help!

Answers


You would create an associative array holding your data, turn it into JSON and return it:

$data = array('var_one' => $var1, 'var_two' => $var2);
$jsonData = json_encode($data);
echo $jsonData;

You can read about manipulating elements with jQuery to learn about inputting the data into your HTML.


Need Your Help

Convert from kilometers, (km), to decimal degrees

python distance spatial haversine

Using scipy.KDTree to do some quick nearest neighbour searches. I'm using KDTree.query_ball_point(pnt, r=some_distance) to do the search.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.