Home  >  Article  >  Web Front-end  >  ajax submit form to database instance

ajax submit form to database instance

小云云
小云云Original
2018-02-23 15:05:215520browse

Submitting a form to the database on a static page is very simple.

<form action="test.php" method="post">
</form>

The disadvantage is that the page will be refreshed and the page will jump. The technology I bring to you today is the ajax form submission. The advantage is that it does not refresh the page, does not jump to the page, and is submitted silently.

First we have to have a form submission page:

index.html
This page consists of two parts
1. Form control
2.jQuery+ajax Processing script
The jQuery script will obtain the form form data and submit it to api.php through post

b317222a318de8270818dffaffdd385c
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
    b2386ffb911b14667cb8f0f91ea547a7login test6e916e0f7d1e588d4f442bf645aedb2f
    a71ddd814d1e6bfc91cd420de31796d5
    997f162832870813a4bba3cd209ffad62cacc6d41bbb37262a98f745aa00fbf0
    8019067d09615e43c7904885b5246f0a
        function insert() {
            $.ajax({
                type: "POST",//方法
                url: "api.php" ,//表单接收url
                data: $('#form1').serialize(),
                success: function () {
                  //提交成功的提示词或者其他反馈代码
                    var result=document.getElementById("success");
                    result.innerHTML="成功!";
                },
                error : function() {
                  //提交失败的提示词或者其他反馈代码
                    var result=document.getElementById("success");
                    result.innerHTML="失败!";
                }
            });
        }
    2cacc6d41bbb37262a98f745aa00fbf0
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
e1d846a265bf300ddc7f4a80b95329f3
    d141580f4a41fe1ee835e89737c04401
        e388a4556c0f65e1904146cc1a846bee0581997439755c399a6c92b9221be45d94b3e26ee717c64999d7867364b1b4a3
        e388a4556c0f65e1904146cc1a846bee15bdbefafd8a17e62bcaf970c51328ab94b3e26ee717c64999d7867364b1b4a3
        e388a4556c0f65e1904146cc1a846bee2cdb61f75629cb4e00b15553abed93b194b3e26ee717c64999d7867364b1b4a3
        e388a4556c0f65e1904146cc1a846bee77361903f13dcd26502e2c12536599cd94b3e26ee717c64999d7867364b1b4a394b3e26ee717c64999d7867364b1b4a3
    f5a47148e367a6035fd7a2faa965022e
94b3e26ee717c64999d7867364b1b4a3
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

The following is the form receiving page
api.php
This page is actually very simple
Just connect to the database
Then insert into the database
The two values ​​inserted into the database are
title and url

17154344e9e2a6fb548e020bf5ec4cb0

Then we need to create a database
The database is named test, and the table Named testdata

The following is a screenshot of the database

ajax submit form to database instance

At this point, this case is completed.
Of course, the above code simply implements the ajax form submission.
But there are still many details that need to be optimized, such as form verification, data encryption, etc. You can expand, learn and improve by yourself.

Related recommendations:

Ajax submission Form form page will still refresh j solution

js submit form form, and Implementation method of passing parameters

js Implementation method of submitting form form and setting form form request path

The above is the detailed content of ajax submit form to database instance. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn