【问题标题】:form not auto submitting表单不自动提交
【发布时间】:2015-05-14 15:43:43
【问题描述】:

我正在使用谷歌地图 api 来获取用户位置。它返回 js 变量中的数据。现在我想把这个js变量转换成php。我已经使用表单提交来转换变量,但问题是表单不是自动提交的。

 <?php
        if(isset($_POST['loc'])){
            ?>
        <script>alert("i am post!");
        </script>
    <?php
            echo "i am post";
            $country_name = $_POST['abc'];
            echo $country_name."i am php";
        }
        else{
            echo "i am php else";
    ?>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript">
        var geocoder;
        var country_name;
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
        }
        //Get the latitude and the longitude;
        function successFunction(position) {
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
            codeLatLng(lat, lng)
        }

        function errorFunction() {
            alert("Geocoder failed");
        }

        function initialize() {
            geocoder = new google.maps.Geocoder();



        }

        function codeLatLng(lat, lng) {

            var latlng = new google.maps.LatLng(lat, lng);
            geocoder.geocode({ 'latLng': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    //console.log(results)
                    if (results[1]) {
                        //formatted address
                       // alert(results[0].formatted_address)
                        //find country name
                        for (var i = 0; i < results[0].address_components.length; i++) {
                            for (var b = 0; b < results[0].address_components[i].types.length; b++) {

                                //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate
                                if (results[0].address_components[i].types[b] == "country") {
                                    //this is the object you are looking for
                                    city = results[0].address_components[i];
                                    break;
                                }
                            }
                        }
                        //city data
                        alert(city.short_name + " " + city.long_name)
                        country_name = city.short_name;
                        document.forms[0].abc.value = country_name;
                        $("#def").submit();
                        document.write("form is submited");

                    } else {
                        alert("No results found");
                    }
                } else {
                    alert("Geocoder failed due to: " + status);
                }
            });
        }
    </script> 

    <body onload="initialize()"> 
        <form id="def" action="Db1.php" method="POST">
            <input name="abc" id="abc" value="" />
            <input type="submit" name="loc" />
        </form>
    </body> 
    <script>
        $(function () {
            alert(" i am jquery function");
            window.onload(function () {
                $("#def").submit();
            });

        });
    </script>
    <?php 

        }
    ?>

【问题讨论】:

  • 什么鬼 -2 没有回应:(
  • 您忘记包含jquery.. 请在包含谷歌地图js之前包含它
  • $(function() 在没有 jquery 的情况下失败。 :P
  • 我已经包含了 jquery。
  • 您是否查看了浏览器控制台中是否有请求/响应?

标签: php jquery forms


【解决方案1】:

替换你的脚本

 <script>
        $(function () {
            alert(" i am jquery function");
            window.onload(function () {
                $("#def").submit();
            });

        });
    </script>

有了这个

     <script>
                document.getElementById('def').submit();
     </script>

编辑 如果你想使用 jquery 提交而不是这样做

$( "#def" )[0].submit();   

【讨论】:

  • 无缘无故投反对票??我试过这段代码,它正在工作....与整个 OP 脚本
  • 我没有DV,但我明白为什么会这样。使用 jQuery 函数进行测试(我将 alert() 交换为 console.log())并确保输入有一个值,提交没有问题。
  • 是的.. 可能是她同时使用了两个嵌套的自动加载功能$(function() { and window.onload(function() {....
  • @Nishant_Solanki 感谢您的回答,但它不起作用。实际上body标签之后的脚本代码没有执行。 “我是 jquery 函数”警报没有出现。
  • @Ayesha 仅使用 $( "#def" )[0].submit(); ... 从您的上一个脚本中删除其他代码....
猜你喜欢
  • 2015-09-24
  • 1970-01-01
  • 2018-01-15
  • 2013-03-15
  • 2011-02-05
  • 2010-11-25
  • 1970-01-01
相关资源
最近更新 更多