tailieunhanh - Giải pháp thiết kế web động với PHP - p 29
PAGES THAT REMEMBER: SIMPLE LOGIN AND MULTIPAGE FORMS If the password fails either test or both, the $_errors property contains at least one element, which PHP treats as intrinsicly true. The final line in the check() method uses the $_errors property as the condition with the ternary operator. If any errors are found, the check() method returns false indicating that the password failed validation. Otherwise, it returns true. The getErrors() public method simply returns the array of error messages. 4. 5. Save , and switch to . In , create a Ps2_CheckPassword object, passing $password as the argument. Then call the. | PAGES THAT REMEMBER SIMPLE LOGIN AND MULTIPAGE FORMS If the password fails either test or both the _errors property contains at least one element which PHP treats as intrinsicly true. The final line in the check method uses the _errors property as the condition with the ternary operator. If any errors are found the check method returns false indicating that the password failed validation. Otherwise it returns true. The getErrors public method simply returns the array of error messages. 4. Save and switch to register .php. 5. In create a Ps2_CheckPassword object passing password as the argument. Then call the check method and handle the result like this require_once . classes Ps2 checkPwd new Ps2_CheckPassword password passwordOK checkPwd- check if passwordOK result array Password OK else result checkPwd- getErrors 6. The second argument to the Ps2_CheckPassword constructor is optional so leaving it out sets the minimum number of characters to the default 8. The result of the check method is assigned to passwordOK. If it returns true a single-element array reporting that the password is OK is assigned to result. Otherwise the getErrors method is used to retrieve the array of errors from the checkPwd object. The single-element array will be used only to test the class. Once testing is complete it will be replaced by the script that registers the user. 7. Add the following PHP code block just above the form in the body of the page h1 Register User h1 php if isset result echo ul foreach result as item echo li item li echo ul form action method post id form1 This displays the results of the password test as an unordered list after the form has been submitted. 261 CHAPTER 9 Download from Wow eBook 8. Save and load it in a browser. Test the Ps2_CheckPassword class by clicking the Register button without filling in any of the fields. You should see a message informing you that the password .
đang nạp các trang xem trước