简体   繁体   English

PHP - 单击按钮后通过 Ajax 函数提交表单

[英]PHP - Submit form through Ajax function after button click

I have a form that is currently submitted to a database via an Ajax function after submitting.我有一个表单,当前提交后通过 Ajax 函数提交到数据库。 However, I want to change this by adding a 'Save' button that will save the details to a different table in the database that the user can return too.但是,我想通过添加一个“保存”按钮来更改这一点,该按钮会将详细信息保存到用户也可以返回的数据库中的不同表中。

This is my current Ajax function...这是我当前的 Ajax 函数...

$(function() {
$("#newForm").submit(function(event){
event.preventDefault();
$('.modal').hide();
//var form = document.querySelector('#newForm'); // Find the <form> element
var formData = new FormData($("#newForm")[0]); // Wrap form contents
$('.modal').show();
//function to submit
$.ajax({
            type: "post",
            url: "adminChanges.php",
            data: formData,
                    dataType: 'json',
            cache: false,
            contentType: false,
            processData: false,
                    success: function(data){
                        if(data.error==''){
                            console.log(data);
                            $('#newForm')[0].reset();
                            alert('Job Saved');
                $('#myModal').hide();
              //document.location.href = 'weeklyAuditIndex.php';
                        }
              else
              {
                alert(data.error)
              }
                    },
                    error: function(data){
                        console.log(data);
              $('.modal').hide();
                    }
            });

  });
})

I've tried following a method where I change buttons input types from submit to buttons like so我试过按照一种方法将按钮输入类型从提交更改为这样的按钮

<input type="button" value="Submit" name="submit_button">
<input type="button" value="Save" name="save_button">

And I've copied the above Ajax function, that when save_button is clicked it will post to adminChangesTemp.php.我复制了上面的 Ajax 函数,当点击 save_button 时,它会发布到 adminChangesTemp.php。 Then updated the functions to something like below然后将功能更新为如下所示

$("#save_button").on("click", function() {
$("#newForm").submit(function(event){
etc...

However nothing is now happening when I click either button.但是,当我单击任一按钮时,现在什么也没有发生。

我看到您正在使用 id #save_button 但在输入字段上,保存按钮输入上没有 id="save_button"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM