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

时间:2020-03-16 15:06:00

标签: php html ajax

我有一个表单,当前已在提交后通过Ajax函数提交到数据库。但是,我想通过添加一个“保存”按钮来更改此设置,该按钮会将详细信息保存到数据库中用户也可以返回的其他表中。

这是我当前的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();
                    }
            });

  });
})

我尝试过一种方法,可以将按钮的输入类型从提交更改为按钮,就像这样

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

我已经复制了上面的Ajax函数,当单击save_button时,它将发布到adminChangesTemp.php。然后将功能更新为如下所示

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

但是,当我单击任一按钮时,什么也没有发生。

1 个答案:

答案 0 :(得分:0)

我看到您使用的是ID #save_button,但在输入字段上,“保存”按钮输入上没有id =“ save_button”

相关问题