27.10.2015 Views

AJAX and PHP

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 4<br />

}<br />

{<br />

}<br />

$_POST['txtBthDay'] . '#' .<br />

$_POST['txtBthYear']))<br />

$_SESSION['errors']['txtBthYear'] = 'error';<br />

$errorsExist = 1;<br />

// Validate email<br />

if (!$this->validateEmail($_POST['txtEmail']))<br />

{<br />

$_SESSION['errors']['txtEmail'] = 'error';<br />

$errorsExist = 1;<br />

}<br />

// Validate phone<br />

if (!$this->validatePhone($_POST['txtPhone']))<br />

{<br />

$_SESSION['errors']['txtPhone'] = 'error';<br />

$errorsExist = 1;<br />

}<br />

// Validate read terms<br />

if (!isset($_POST['chkReadTerms']) ||<br />

!$this->validateReadTerms($_POST['chkReadTerms']))<br />

{<br />

$_SESSION['errors']['chkReadTerms'] = 'error';<br />

$_SESSION['values']['chkReadTerms'] = '';<br />

$errorsExist = 1;<br />

}<br />

// If no errors are found, point to a successful validation page<br />

if ($errorsExist == 0)<br />

{<br />

return 'allok.php';<br />

}<br />

else<br />

{<br />

// If errors are found, save current user input<br />

foreach ($_POST as $key => $value)<br />

{<br />

$_SESSION['values'][$key] = $_POST[$key];<br />

}<br />

return 'index.php';<br />

}<br />

// validate user name (must be empty, <strong>and</strong> must not be already registered)<br />

private function validateUserName($value)<br />

{<br />

// trim <strong>and</strong> escape input value<br />

$value = $this->mMysqli->real_escape_string(trim($value));<br />

// empty user name is not valid<br />

if ($value == null)<br />

return 0; // not valid<br />

// check if the username exists in the database<br />

$query = $this->mMysqli->query('SELECT user_name FROM users ' .<br />

'WHERE user_name="' . $value . '"');<br />

if ($this->mMysqli->affected_rows > 0)<br />

return '0'; // not valid<br />

else<br />

return '1'; // valid<br />

}<br />

// validate name<br />

137<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!