Skip to content

Commit

Permalink
Login example php files
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.php.net/repository/pear/packages/HTML_AJAX/trunk@221693 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information
David Coallier committed Oct 13, 2006
1 parent 046b1a8 commit 483f26b
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/login/php/auto_server.php
@@ -0,0 +1,34 @@
<?php
/**
* Very simple form script with error handling.
*
* @category HTML
* @package AJAX
* @author Gilles van den Hoven <gilles@webunity.nl>
* @copyright 2005 Gilles van den Hoven
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version Release: @package_version@
* @link http://pear.php.net/package/HTML_AJAX
*/

// include the server class
include 'HTML/AJAX/Server.php';

// extend HTML_AJAX_Server creating our own custom one with init{ClassName} methods for each class it supports calls on
class LoginServer extends HTML_AJAX_Server {
// this flag must be set to on init methods
var $initMethods = true;

// init method for the test class, includes needed files an registers it for ajax
function initLogin() {
include 'class.login.php';
$this->registerClass(new login(), 'login', array('validate'));
}
}

// create an instance of our test server
$server = new LoginServer();

// handle requests as needed
$server->handleRequest();
?>
128 changes: 128 additions & 0 deletions examples/login/php/class.login.php
@@ -0,0 +1,128 @@
<?php
// Includes
require_once 'HTML/AJAX/Action.php';

// Error messages
define('ERR_USERNAME_EMPTY', 'You forgot to enter a username, please try again');
define('ERR_USERNAME_INVALID', 'The username you entered is invalid. Please try "peter" (without quotes)');
define('ERR_PASSWORD_EMPTY', 'You forgot to enter a password, please try again');
define('ERR_PASSWORD_INVALID', 'The password you entered is invalid. Please try "gabriel" (without quotes)');
define('ERR_EMAIL_EMPTY', 'You forgot to enter an e-mail address');
define('ERR_EMAIL_INVALID', 'The e-mail address you entered is invalid. Please enter a valid e-mail address.');

/**
* Login class used in the "login form" example
* Please note: Constructors and private methods marked with _ are never exported in proxies to JavaScript
*
* @category HTML
* @package AJAX
* @author Gilles van den Hoven <gilles@webunity.nl>
* @copyright 2005 Gilles van den Hoven
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @version Release: @package_version@
* @link http://pear.php.net/package/HTML_AJAX
*/
class login {
/**
* PHP5 requires a constructor
*/
function login() {
}

/**
* Checks the proper syntax
*/
function _checkEmail($strEmail) {
return (preg_match( '/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i' , $strEmail));
}

/**
* Checks if the passed values are correct.
*
* @param array the form object
*/
function validate($arrayForm) {
//--------------------------------------------------
// Initialize function
//--------------------------------------------------
// Array to hold the messages
$arrMessages = array();

// Set all form values that they validated. Could be improved by analyzing
// the values passed in $objForm and setting values accordingly.
$arrValidated = array();
$arrValidated['username'] = true;
$arrValidated['password'] = true;
$arrValidated['email'] = true;

// Never trust the values passed by users :)
$objForm = new stdClass();
$objForm->username = trim($arrayForm['username']);
$objForm->password = trim($arrayForm['password']);
$objForm->email = trim($arrayForm['email']);

//--------------------------------------------------
// Check values
//--------------------------------------------------
// Check username
if ($objForm->username == '') {
$arrMessages[] = ERR_USERNAME_EMPTY;
$arrValidated['username'] = false;
} else if ($objForm->username != 'peter') {
$arrMessages[] = ERR_USERNAME_INVALID;
$arrValidated['username'] = false;
}

// Check password
if ($objForm->password == '') {
$arrMessages[] = ERR_PASSWORD_EMPTY;
$arrValidated['password'] = false;
} else if ($objForm->password != 'gabriel') {
$arrMessages[] = ERR_PASSWORD_INVALID;
$arrValidated['password'] = false;
}

// Check email
if ($objForm->email == '') {
$arrMessages[] = ERR_EMAIL_EMPTY;
$arrValidated['email'] = false;
} else if ($this->_checkEmail($objForm->email) == false) {
$arrMessages[] = ERR_EMAIL_INVALID;
$arrValidated['email'] = false;
}

//--------------------------------------------------
// Finalize function
//--------------------------------------------------
// Create the message list
$strMessages = '';
if (count($arrMessages) > 0) {
$strMessages = '<ul>';
foreach ($arrMessages as $strTemp) {
$strMessages.= '<li>'.$strTemp.'</li>';
}
$strMessages.= '</ul>';
}

// Create a response object
$objResponse = new HTML_AJAX_Action();

// Assign the messages
$objResponse->assignAttr('messages', 'innerHTML', $strMessages);
$objResponse->insertScript("toggleElement('messages', ".(($strMessages != '') ? 1 : 0).");");

// Generate the scripts to update the form elements' label and input element
foreach ($arrValidated as $strKey => $blnValue) {
$objResponse->insertScript("setElement('".$strKey."', ".(($blnValue) ? '1' : '0').");");
}

// Test for no messages
if ($strMessages == "") {
$objResponse->insertAlert("Well done chap!");
}

// And ready! :)
return $objResponse;
}
}
?>

0 comments on commit 483f26b

Please sign in to comment.