【问题标题】:call post method without submit button causing infinite loop在没有提交按钮的情况下调用 post 方法导致无限循环
【发布时间】:2015-07-12 22:07:22
【问题描述】:

我有一个表格,我想从中获得纬度和经度,我想在没有提交按钮的情况下加载它。

<form method="post" id="myform" action="" >
  <input type='hidden' value='' name='latitude'/>
  <input type='hidden' value='' name='longitude'/>
</form>

我试过了

document.getElementById("myForm").submit();

但它会导致无限循环,我希望它在同一页面上,所以我不采取行动。

我添加了这个功能,但还是不行。

function send_data(lat, lon) {
    document.getElementById('place_lon').innerHTML = lat + ' : ' + lon;
   //     $("input[name='lattitude']").val(lat);
    //    $("input[name='longitude']").val(lon);
    $j = jQuery.noConflict();

    $j(document).ready(function() {
        $j('#myform').submit(submit_myform);
   });

    function submit_myform() {
             $j.ajax({
                url: window.location.href,
                type: 'POST',
                data: $j(this).serialize(),
                dataType: 'json', //data type of Ajax response expected from server
                success: myform_success //callback to handle Ajax response and submit to Paypal
            });
            return false;//prevent normal browser submission, since the form is submitted in the callback
    }

    function myform_success(response) {
        //this is called whenever the ajax request returns a "200 Ok" http header
        //manipulate the form as you wish
        $("input[name='latitude']").val(lat);
        $("input[name='longitude']").val(lon);
       // $j('#lattitude').val(lat);
      //  $j('#longitude').val(lon);
        //submit the form (to the form's action attribute)
        document.forms['#myform'].submit();
    } 

}

【问题讨论】:

  • 什么时候执行提交?
  • 您的页面可能会不断地向自己提交自己。尝试在 document.getElementById("myForm").submit(); 上添加一些条件;
  • 正在加载我的表单...
  • 我应该使用哪种类型的条件...

标签: javascript php html dom


【解决方案1】:

发生的情况是,一旦加载表单,它就会自行提交,然后再次加载并提交等等。

尝试使用 AJAX 请求向服务器发送数据。

$.ajax({
  type: "POST",
  url: window.location.href,
  data: $('#myForm').serialize()
});

【讨论】:

  • 我试过这个并在脚本的代码中添加这个。我对 AJAX 不太了解,但我仍然没有在 $_POST["latitude"] 和 $_POST["longitude"] 中获得任何价值..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-04
  • 2012-04-18
  • 1970-01-01
  • 1970-01-01
  • 2012-07-30
  • 2017-07-13
相关资源
最近更新 更多