使用Jquery AJAX提交HTML表单

Oli*_*ira 100 ajax jquery form-submit

我试图使用这个例子使用AJAX提交HTML表单.

我的HTML代码:

<form id="formoid" action="studentFormInsert.php" title="" method="post">
    <div>
        <label class="title">First Name</label>
        <input type="text" id="name" name="name" >
    </div>
    <div>
        <label class="title">Name</label>
        <input type="text" id="name2" name="name2" >
    </div>
    <div>
        <input type="submit" id="submitButton"  name="submitButton" value="Submit">
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

我的剧本:

<script type="text/javascript">
    $(document).ready(function() { 
        $('#formoid').ajaxForm(function() { 
            alert("Thank you for your comment!"); 
        }); 
    });
</script>
Run Code Online (Sandbox Code Playgroud)

这不起作用,我甚至没有收到警报消息,当我提交时我不想重定向到另一个页面,我只想显示警报消息.

有一个简单的方法吗?

PS:我有几个领域,我刚刚把两个作为一个例子.

abc*_*123 168

AJAX的简要说明

AJAX只是Asyncronous Javascript或XML(在大多数新情况下JSON).因为我们正在执行ASYNC任务,所以我们可能会为用户提供更愉快的UI体验.在这个特定情况下,我们正在使用AJAX进行FORM提交.

真的很快有4个一般的网络行动GET,POST,PUT,和DELETE; 这些直接与对应SELECT/Retreiving DATA,INSERTING DATA,UPDATING/UPSERTING DATA,和DELETING DATA.默认的HTML/ASP.Net webform/PHP/Python或任何其他form操作是"提交",这是一个POST操作.因此,下面将描述做POST.但有时使用http,您可能需要一个不同的操作,并可能想要使用.ajax.

我的代码专门为您准备(在代码注释中描述):

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
    <form id="formoid" action="studentFormInsert.php" title="" method="post">
        <div>
            <label class="title">First Name</label>
            <input type="text" id="name" name="name" >
        </div>
        <div>
            <label class="title">Name</label>
            <input type="text" id="name2" name="name2" >
        </div>
        <div>
            <input type="submit" id="submitButton"  name="submitButton" value="Submit">
        </div>
 </form>
<script type='text/javascript'>
    /* attach a submit handler to the form */
    $("#formoid").submit(function(event) {

      /* stop form from submitting normally */
      event.preventDefault();

      /* get the action attribute from the <form action=""> element */
      var $form = $( this ),
          url = $form.attr( 'action' );

      /* Send the data using post with element id name and name2*/
      var posting = $.post( url, { name: $('#name').val(), name2: $('#name2').val() } );

      /* Alerts the results */
      posting.done(function( data ) {
        alert('success');
      });
    });
</script>

</body>
</html> 
Run Code Online (Sandbox Code Playgroud)

文档

来自jQuery网站$.post文档.

示例:使用ajax请求发送表单数据

$.post("test.php", $("#testform").serialize());
Run Code Online (Sandbox Code Playgroud)

示例:使用ajax发布表单并将结果放入div中

<!DOCTYPE html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    </head>
    <body>
        <form action="/" id="searchForm">
            <input type="text" name="s" placeholder="Search..." />
            <input type="submit" value="Search" />
        </form>
        <!-- the result of the search will be rendered inside this div -->
        <div id="result"></div>
        <script>
            /* attach a submit handler to the form */
            $("#searchForm").submit(function(event) {

                /* stop form from submitting normally */
                event.preventDefault();

                /* get some values from elements on the page: */
                var $form = $(this),
                    term = $form.find('input[name="s"]').val(),
                    url = $form.attr('action');

                /* Send the data using post */
                var posting = $.post(url, {
                    s: term
                });

                /* Put the results in a div */
                posting.done(function(data) {
                    var content = $(data).find('#content');
                    $("#result").empty().append(content);
                });
            });
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

重要的提示

如果不使用OAuth或至少使用HTTPS(TLS/SSL),请不要将此方法用于安全数据(信用卡号,SSN,PCI,HIPAA或登录相关的任何内容)


Var*_*n S 25

var postData = "text";
      $.ajax({
            type: "post",
            url: "url",
            data: postData,
            contentType: "application/x-www-form-urlencoded",
            success: function(responseData, textStatus, jqXHR) {
                alert("data saved")
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        })
Run Code Online (Sandbox Code Playgroud)

  • 很好的例子和写得很好,请发一些答案,虽然不只是示例代码没有解释. (3认同)
  • 我不知道如何将其与我希望发布的表单联系起来 (2认同)