通过表单提交获取用户位置 [英] Getting user location with form submit

查看:130
本文介绍了通过表单提交获取用户位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用HTML5地理位置和用户提交表单后,我想存储他的经纬度,但出现了问题。



我做了一个onclick =事件在提交按钮上,当按下按钮时,js将获得协调并将它们放到表单中的隐藏字段中,表单将发送到php,但每当我提交表单时,小弹出窗口就会出现要求获得该位置的权限,并且它可能会因为刷新页面的php而消失,我该怎么办?正如我已经说过的,当你点击提交表单按钮时,js将被触发并在权限被接受后请求许可,隐藏字段将填充协调数据,然后表单将被发送到服务器,但是,在我按下后按钮,然后关于允许位置将弹出一秒钟,并立即消失和页面将刷新没有结果的弹出窗口,我该如何解决这个问题?

解决解决此问题的一个简单方法是运行JS代码,该代码检测位置,并在窗体第一次加载时在页面中请求权限,而不是在提交



你的问题是,你想在表单提交时同时执行javascript。除非您重写默认行为,否则无论在onclick事件中运行哪些脚本,表单都会提交,并且不会等待它们。另一种方式是覆盖默认的提交行为。假设你已经加载了jQuery,就像这样处理点击:

  $(function(){
$(# mySubmitButton)。click(function(event){
event.preventDefault(); //停止表单提交
//在这里写代码请求位置并将其放入隐藏字段
$(#myForm)。submit(); //现在只有我们手动提交表单了,
});
});


Hello im using HTML5 geolocation and after the user submits the form i want to store his longtitude and latitude but a problem has appeared

i made an onclick="" event on the submit button and when the button is pressed js will get the coordinations and put them to hidden fields in the form and the form will be sent to the php but , whenever i submit the form , the small popup will come out and it asks for permission to get the location and it imediately dissapears thats probably because of the php that refreshed the page , what should i do ? as i already said when you click the submit form button js will be triggered and will ask for permission after the permission is accepted the hidden fields will be filled with the coordination data and then the form will be sent to the server but , after i press the button then the popup about allowing the location will go out for one second and imediately dissapear and the page will refresh with no result , how can i fix this ?

解决方案

One simple way to solve this is to run the JS code that detects the location and asks for permission when the form first loads in the page, not when it's submitted.

Your issue is that you're trying to execute javascript simultaneously as the form submits. Unless your override the default behaviour, the form will submit anyway regardless of what scripts are run in the onclick event, and it will not wait for them.

The other way is to override the default submit behaviour. Assuming you have jQuery loaded, handle the click like this:

$(function() {
  $("#mySubmitButton").click(function(event) {
    event.preventDefault(); //stops the form from submitting
    //here write the code which asks for the location and puts it in the hidden fields
   $("#myForm").submit(); //only now do we submit the form, manually.
  });
});

这篇关于通过表单提交获取用户位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆