简体   繁体   English

使用AJAX提交表单无效。 它忽略了ajax

[英]Submitting form with AJAX not working. It ignores ajax

I've never used Ajax before, but from researching and other posts here it looks like it should be able to run a form submit code without having to reload the page, but it doesn't seem to work. 我以前从未使用过Ajax,但是从这里的研究和其他文章看来,它应该能够运行表单提交代码而不必重新加载页面,但是它似乎不起作用。

It just redirects to ajax_submit.php as if the js file isn't there. 它只是重定向到ajax_submit.php,就好像js文件不存在一样。 I was trying to use Ajax to get to ajax_submit without reloading anything. 我试图使用Ajax来获取ajax_submit,而无需重新加载任何内容。

Is what i'm trying to do even possible? 我要做什么甚至有可能吗?

HTML form: HTML形式:

<form class="ajax_form" action="ajax_submit.php" method="post">
<input class="input" id="license" type="text" name="license" placeholder="License" value="<?php echo htmlentities($person['license1']); ?>" />
<input class="input" id="license_number" type="text" name="license_number" placeholder="License number" value="<?php echo htmlentities($person['license_number1']); ?>" />

<input type="submit" class="form_button" name="submit_license1" value="Save"/>
<input type="submit" class="form_button" name="clear1" value="Clear"/>
</form>

in scripts.js file: 在scripts.js文件中:

$(document).ready(function(){
$('.ajax_form').submit(function (event) {
    alert('ok');
    event.preventDefault();
    var form = $(this);
    $.ajax({
        type: "POST",
        url: "ajax_submit.php",//form.attr('action'),
        data: form.serialize(),
        success: function (data) {alert('ok');}
    });
});
});

in ajax_submit.php: 在ajax_submit.php中:

require_once("functions.php");
require_once("session.php");
include("open_db.php");

if(isset($_POST["submit_license1"])){
    //query to insert
}elseif(isset($_POST['clear1'])) {
    //query to delete
}

I have " <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> " in the html head 我在HTML中有“ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> “头

form.serialize() doesn't know which button was used to submit the form, so it can't include any buttons in the result. form.serialize()不知道使用哪个按钮提交表单,因此结果中不能包含任何按钮。 So when the PHP script checks which submit button is set in $_POST , neither of them will match. 因此,当PHP脚本检查$_POST设置了哪个提交按钮时,它们都不匹配。

Instead of using a handler on the submit event, use a click handler on the buttons, and add the button's name and value to the data parameter. 代替在submit事件上使用处理程序,而在按钮上使用单击处理程序,然后将按钮的名称和值添加到data参数中。

$(":submit").click(function(event) {
    alert('ok');
    event.preventDefault();
    var form = $(this.form);
    $.ajax({
        type: "POST",
        url: "ajax_submit.php",//form.attr('action'),
        data: form.serialize() + '&' + this.name + '=' + this.value,
        success: function (data) {alert('ok');}
    });
});

Your ajax call is working perfectly. 您的ajax呼叫运行正常。 You have few conceptual error with your code - 您的代码几乎没有概念错误-

  1. form.serialize() will not attach submit button's info. form.serialize()将不附加提交按钮的信息。

  2. If you want to clear your form, you can do it using something like this 如果您想清除表格,可以使用类似的方法

$('#resetForm').click(function(){ $('.ajax_form')[0].reset(); });

  1. Lastly complete your task & return success or failed value to ajax call using echo like echo 'successful' or echo failed etc. Use an else condition with your code. 最后,使用echoecho 'successful'echo failed等)完成任务并返回成功或失败的值到ajax调用。在代码中使用else条件。 It will be more clearer to you. 对您来说会更清晰。

Remove the "action" and "method" attributes from the form. 从表单中删除“操作”和“方法”属性。 You shouldn't need them. 您不需要它们。

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

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