In certain scenarios, developers may encounter the need to transfer data from JavaScript variables to PHP variables. A compelling instance of this is when obtaining coordinates from Google Maps and aiming to store them within a MySQL database.
Consider a JavaScript variable named lugar that holds latitudinal and longitudinal coordinates. To pass this value to the PHP variable of the same name ($lugar), a seamless approach can be achieved through jQuery's Ajax capabilities.
<code class="html"><script> $.ajax({ url: "save.in.my.database.php", type: "post", dataType: "json", data: { lugar: results[0].geometry.location }, success: function(data) { alert('Saved!'); }, error: function() { alert('Error encountered.'); } }); </script></code>
This script transmits the lugar variable to the PHP script, which then handles the database saving process.
The above is the detailed content of How to Pass JavaScript Variables to PHP Variables to Preserve Google Maps Data. For more information, please follow other related articles on the PHP Chinese website!