Home  >  Article  >  Web Front-end  >  Submit form to database using ajax without refreshing

Submit form to database using ajax without refreshing

php中世界最好的语言
php中世界最好的语言Original
2018-03-30 15:17:161473browse

This time I will bring you the precautions for using ajax to submit the form to the database without refreshing. What are the precautions for using ajax to submit the form to the database without refreshing? The following is a practical case, let's 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>

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 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: $('#form1').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>

The following is the form receiving page

api.php

This page is actually very simple

It isconnect to the database

and theninsert datalibrary

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);
?>

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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Use Ajax and forms to implement the functions required for registered users

ajax to implement dynamic pie charts and columns Detailed explanation of graphic diagram

The above is the detailed content of Submit form to database using ajax without refreshing. 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