Skip to content

Instantly share code, notes, and snippets.

@alash3al
Last active August 29, 2015 14:08
Show Gist options
  • Save alash3al/2d99b06efad69a69c7fe to your computer and use it in GitHub Desktop.
Save alash3al/2d99b06efad69a69c7fe to your computer and use it in GitHub Desktop.
example > form processing
<?php
// here we handle all form errors
$GLOBALS['formE'] = array();
// say that some hit the submit button
// and the form method is POST
if ( isset( $_POST['submit'] ) )
{
// create a shortcut to $_POST var
$in = &$_POST;
// say that there is name="name" and must be not empty
( isset($in['name']) && !empty($in['name']) && strlen(trim($in['name'])) >= 5 ) OR
( $GLOBALS['formE'][] = 'the name length must be at least 5' );
// say that there is name="pass" and must be not empty
( isset($in['pass']) && !empty($in['pass']) && strlen(trim($in['pass'])) >= 5 ) OR
( $GLOBALS['formE'][] = 'the password length must be at least 5' );
// if the errors array was empty, everything would be ok
if ( sizeof ( $GLOBALS['formE'] ) === 0 )
{
// save the form
}
}
?>
<!-- display any error if there -->
<?php foreach ( $GLOBALS['formE'] as &$e ): ?>
<p># <?php echo $e ?></p>
<?php endforeach ?>
<!-- the form --->
<form method="post">
<input type="text" name="name"/>
<input type="text" name="pass"/>
<input type="submit" value="submit" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment