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.