Home > Web Front-end > JS Tutorial > body text

Detailed explanation of using ajax to submit a form to the database (no refresh)

亚连
Release: 2018-05-21 16:17:22
Original
2648 people have browsed it

This article mainly introduces you to the relevant information about using ajax to submit the form to the database (without refreshing). The article introduces it in detail through the sample code. It has certain reference learning value for everyone's study or work. I feel Interested friends can come and take a look.

Everyone should know that submitting a form to the database on a static page is very simple.

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

The disadvantage is that the page will be refreshed and the page will jump.

The technology brought 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.

As for what ajax is, go to Baidu to find out for yourself.

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
 <title>login test</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
 <script type="text/javascript">
  function insert() {
   $.ajax({
    type: "POST",//方法
    url: "api.php" ,//表单接收url
    data: $(&#39;#form1&#39;).serialize(),
    success: function () {
     //提交成功的提示词或者其他反馈代码
     var result=document.getElementById("success");
     result.innerHTML="成功!";
    },
    error : function() {
     //提交失败的提示词或者其他反馈代码
     var result=document.getElementById("success");
     result.innerHTML="失败!";
    }
   });
  }
 </script>
</head>
<body>
<p id="form-p">
 <form id="form1" onsubmit="return false" action="##" method="post">
  <p><input name="title" id="title" type="text" value="title"/></p>
  <p><input name="url" id="url" type="text" value="url"/></p>
  <p><input type="button" value="插入" onclick="insert()"></p>
  <p><p id="success"></p></p>
 </form>
</p>
</body>
</html>
Copy after login

The following is the form receiving page

api.php

This page is actually very simple

It is to connect to the database

Then insert into the database

The two values ​​inserted into the database are

title and url

<?php
$title = $_POST[&#39;title&#39;];
$url = $_POST[&#39;url&#39;];
$con = mysql_connect("localhost","root","root");
if (!$con)
 {
 die(&#39;Could not connect: &#39; . mysql_error());
 }
mysql_select_db("test", $con);
mysql_query("INSERT INTO testdata (title, url) 
VALUES (&#39;$title&#39;, &#39;$url&#39;)");

mysql_close($con);
?>
Copy after login

Then we need to create a database

The database name is test and the table name is testdata

The following is a screenshot of the database

At this point, this case is completed.

Of course, the above code simply implements the ajax submission form

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.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

ajaxWhat are the steps required to send data to the server

ajaxImplementation of login function

How to cancel jQueryajaxRequest

The above is the detailed content of Detailed explanation of using ajax to submit a form to the database (no refresh). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!