php - Ajax请求触发提交....为什么?

标签 php jquery ajax forms submit

所以我有一些看起来可能有点小但这里的东西。

这一切都是使用 jQuery 完成的

我有一个函数“cont_refresh”,用于刷新页面内容等,其中包含一个 AJAX 请求,用于将变量传递到外部文档/脚本进行处理。

除此之外,我还有一个匿名函数,应该在表单提交时触发...这意味着完全不相关,并且不应在按下此按钮时发生。

但是,当我运行“cont_refresh”时,提交函数会被触发,似乎触发提交函数的时间点是我开始发出ajax请求时。

最终发生的是我向 ajax 请求发出请求,第一个请求只是在服务器上运行一个函数,第二个请求是提交我的表单...我不希望后者发生。

有两个按钮可以分别处理这两件事,一个是提交按钮,另一个与此问题相关的不是提交按钮。

为什么?我该如何防止这种情况发生?

您可能会问,为什么这个 clown 有两个功能,基本上做同样的事情,但这是另一个主题,我会优雅地要求您为此提出一个新帖子;)。

/*
  This is what I want to run... it does but fails then.....(see below this function)
*/
function cont_refresh(form,params,url,e,modal){
    $(".ng-loading-container").toggleClass("ng-loading-container-show");
   
    if(modal === 'undefined'){
        modal = false;
    }
    var request = $.ajax({
        method: 'post',
        url: url,
        data: params,
        success: function(result){
            
          if(e !== '' && e !== 'undefined'){
                $(e).html(result);    
            } else {
                alert('Success');
            }
            
        },
        error: function(){
            alert('Page Error')
            $(".ng-loading-container").toggleClass("ng-loading-container-show");
        },
        }).fail(function() {
            alert( "Something went wrong!" );
            $(".ng-loading-container").toggleClass("ng-loading-container-show");
        })
    // end of cont_refresh()
}

/*
.... This runs. Which is not my intention!
*/

$('form').submit(function (e){
    e.preventDefault();
    submit_form(e.target);
    // there is an additional function that handles any submition from this point but have not included it as .
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Rightly or wrongly the intention here is to run a stored SQL procudure.<br>
<p>The reason the statements are stored in the db is so anyone (with access) can add and edit these statements or functions from my front end application.</p>

<form role="form" action="myscript.php" method="post">
  <input type="hidden" name="stp_tsk_id" id="stp_tsk_id" class="form-control" value="34">
  <div class="form-group col-md-12">
        <label for="stp_function">Function (SQL statement)</label>
        <textarea class="form-control" name="stp_function" id="stp_function" rows="3">SELECT * FROM SOME_TABLE</textarea>
</div>
<!-- this button should trigger only the function it is calling onclick -->
<button role="button" 
        class="btn btn-default btn-md"
        onclick="cont_refresh('',{'id':34},'app/handlers/process_steps.php')">
                Run this Step
        </button> 
  
 <!-- There is a second "submit" button but not relevent for this query -->
  <button role="submit" class="btn btn-default btn-md">Save</button>
 </form>

最佳答案

好的,你可以试试这个...

为您的表单指定一个 ID,例如

<form role="form" action="myscript.php" method="post" id="myForm">

但是请随意使用更有创意的 ID 名称...

并将您的事件更改为

$('#myForm').submit(function (e){
    e.preventDefault();
    submit_form(e.target);
    // there is an additional function that handles any submition from this point but have not included it as .
});

看看这是否有帮助。

关于php - Ajax请求触发提交....为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40018462/

相关文章:

php - 存储在 MySQL 中时字符发生变化

php - 我如何使用 MATCH/AGAINST 创建简单的搜索?

PHP 通知 : fwrite(): send failed with errno=32 Broken pipe

javascript - PHP 表单发送重复的电子邮件

javascript - noUiSlider - 限制值

javascript - 如何使用 w3c 验证器 API

javascript - 如何处理 Parse.com 中的 URL 变量?

php创建DOM元素并使用ajax发送

PHP 从 HTML5 获取数据属性

ASP.NET - 将 JSON 从 jQuery 传递到 ASHX