Use ajax to submit the form to the database for detailed explanation (without refreshing), ajaxform

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.