Use ajax to submit the form to the database for detailed explanation (without refreshing), ajaxform
As we all know, submitting a form to a database on a static page is simple.
<form action="test.php" method="post"></form>
This disadvantage is that the page will be refreshed and will jump to the page.
The technology we bring to you today is ajax form submission.
The advantage is that you do not click New pages, do not jump to pages, and submit them silently.
As for what ajax is, go to Baidu to learn about it.
First, we need to have a form submission page:
Index.html
This page consists of two parts
1. Form Controls
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">
The following is the form receiving page.
Api. php
This page is actually very simple
Is to connect to the database
Then insert the database
The two values of the inserted database are
Title and url
<?php$title = $_POST['title'];$url = $_POST['url'];$con = mysql_connect("localhost","root","root");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("test", $con);mysql_query("INSERT INTO testdata (title, url) VALUES ('$title', '$url')");mysql_close($con);?>
Then we need to build a database.
The database name is test and the table name is testdata.
Below is the database
This case has been completed.
Of course, the above Code simply implements ajax to submit form
However, there are still many details to be optimized, such as form verification and data encryption, which can be expanded and improved by yourself.
Summary
The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.