Accessing a Javascript's Variables: Problem involving Javascript, JQuery, and PHP/MySQL & Scope
I'm learning how to build a WordPress Google Map plugin. I've got a map with a form. The user makes selections on the form, data will be pulled from a My开发者_如何学编程SQL database, and then markers will be added to the map.
I've got 3 main parts to my project:
a) A PHP file that has the form and the MySQL SELECT statement for handling the form's selections. b) A Javascript script that builds the Google Map, and calculates the map's bounds c) A jQuery script that has datepickers. It also serializes the form's selections and posts them to the PHP page.
My problem is that, in addition to the form data, I need the latlongs of the 4 corners of the Google Map in order to select the data from the database. I know how to calculate the bounds using Google Maps' functions in script (b). My problem is that I don't know how to return the variables in (b)
var maxLat = ne.lat();
var maxLong = ne.lng();
var minLat = sw.lat();
var minLong = sw.lng();
back to the PHP page. These values aren't in the form that I'm serializing. They are in the javascript script file (b), not the jQuery script (c) that has the post functions. Also, the variables are calculated after the map loads. So, I can't just leave the variables outside of a function and access them from script (c).
What's the best way to give the PHP access to these variables?
Should I try to do a separate post for these values? Is there a way to allow script (c) access to the variables in script (b), and then add them on to the the form's post data somehow?
Please advise what's the best way to tackle this problem. Thank you.
If there is no possibility to let (c) wait for (b), another POST will probably have to do. You can use jQuery's $.post for that. POSTs are quite easy with that. Just be sure to pass an array of your Lons and Lats to it. You may also supply a callback function that will be executed once the POST succeeded.
精彩评论