[英]How can I pass a JavaScript variable generated from an API as part of a form to a php variable?
I have variables generated with a JS Script that I want to pass back to the PHP program that called the script, and trying to figure out the best method for doing that.我有一个 JS 脚本生成的变量,我想将这些变量传递回调用该脚本的 PHP 程序,并试图找出最好的方法。 Because there are 4-5 variables, I do not want to pass it to php using the url.因为有 4-5 个变量,我不想使用 url 将其传递给 php。
The form is using PHP Form Builder and is correctly passing back one variable variable from a Google Maps API JS Script.该表单使用 PHP 表单生成器,并且正确地从 Google 地图 API JS 脚本传回一个变量变量。 The PHP code to set up form: PHP代码设置表格:
$form->addIcon('town-req', '<i class="fa fa-map-marker" aria-hidden="true"></i>', 'before');
$form->addInput('text', 'town-req', '', '', 'placeholder=Your town,onFocus=\'geolocate()\', required');
The values are stored in a MySQL DB using:这些值存储在 MySQL DB 中,使用:
= Mysql::SQLValue($_POST['town-req']);
Within the script, I can assign the other variables I want to pass back to the PHP, such as latitude and longitude:在脚本中,我可以分配我想要传递回 PHP 的其他变量,例如纬度和经度:
var lat = place.geometry.location.lat(),
lng = place.geometry.location.lng();
Is it possible to pass these variables such as “lat” to a $_POST as a hidden field in the form?是否可以将这些变量(例如“lat”)作为表单中的隐藏字段传递给 $_POST?
Or is AJAX is the best way to accomplish this?还是 AJAX 是实现这一目标的最佳方法? Is there an example that can help?有没有可以提供帮助的例子?
I was able to use ajax for this - it was actually much easier than I thought!我能够为此使用 ajax - 它实际上比我想象的要容易得多!
$.ajax({
type: 'POST',
data: {lng:lng},
success: function(data) {
console.log(lng);
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.