Html file:
<Form action = "/index. php/welcome/personpost">
................
<Input type = "submit" id = "personsub" value = "submit"/>
</Form>
Js file:
$ (Document). ready (function ()
{
$ ("# Personsub"). live ('click', function ()
{
If ($ ("# oldpassword"). val () = "")
{
Alert ("the old password cannot be blank! ");
Return false;
}
Var ajaxres = 0;
Var oldpsw = $ ("# oldpassword"). val ();
$. Ajax (
{
Type: "post ",
Url: "/index. php/welcome/checkpsw", // check whether the password is correct
Data: {"oldpsw": oldpsw },
Datatype: "text ",
Async: false, // Note: The default value is asynchronous. if false is synchronous, wait until success is complete and then run the ajax command (if (ajaxres =-1) {return false ;}. Otherwise, the ajaxres variable cannot be correctly assigned a value.
Success: function (result)
{
If (result = "-1") // The password is incorrect.
{
Ajaxres =-1;
Alert ("Incorrect old password. Please try again! ");
$ ("# Oldpassword"). val ("");
$ ("# Oldpassword"). focus ();
// Return false // here, return false is useless
}
}
});
If (ajaxres =-1)
{
Return false; // return false indicates that the form is not submitted and maintained on this page.
}
// If ajaxres! =-1, the form is submitted
});
});