When Ajax submits a form, the verification code automatically verifies the php backend verification code detection _ php instance

Source: Internet
Author: User
The verification code is automatically verified when Ajax submits a form. if the verification code is correct, no form is submitted. the verification code is updated and the php backend verification code is detected, interested partners can refer to this article to show how to implement form submission through the source code, the verification code first checks the correctness, if not, do not submit the form, update the verification code.

1. front-end codeIndex.html

  Verification code submitted for self-verification 
  
 Script (function ($) {$ (document ). ready (function () {$ (". submitBtn "). click (function () {var obj = $ (this); $. ajax ({url: 'checkcode. php ', type: 'post', data: {code: $. trim ($ ("input [name = code]"). val ()}, dataType: 'json', async: false, success: function (result) {if (result. status = 1) {obj. parents ('form '). submit (); // The correct form for submitting the verification code} else {$ (". code-img "). click (); $ (". yzmtips "pai.html ('verification code error! '); SetTimeout (function () {$ (". yzmtips "). empty () ;}, 3000) ;}, error: function (msg) {$ (". yzmtips "pai.html ('Error: '+ msg. toSource () ;}}) return false ;}}) ;}( jQuery); script

2. backend verification code detectionCheckcode. php

<? Php/*** user verification code file * @ Author: Zjmainstay * @ version: 1.0 * @ creatdate: 2013-10-4 */session_start (); echo json_encode (array ('status' => (int) ($ _ SESSION ["CHECKCODE"] == _ POST ['code']); exit;

Source code: Ajax automatically verifies the verification code when submitting a form

The above is all the content of this article. I hope it will be helpful to everyone's learning, and I hope you can support your own home.

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.