Code Samples

POST Requests

Case: a form with Name, Email and Message is posted. We want to get the values, validate and prepare a SQL query.

// Init. class
$Input = new Input;
 
$error = FALSE;
 
// As "is_valid" returns NULL when the data is not set,
// this is what we are checking.
if ( ! is_null ($Input->post ('Name')->is_valid()) ) {
   $Name = $Input->post ('Name');
   $Email = $Input->post ('Email');
   $Msg = $Input->post ('Msg');    
 
   if ($Name->validate ('is_alnum')) === FALSE) {
        $error = TRUE;
    } elseif ($Email->validate ('valid_email') === FALSE) {
        $error = TRUE;
    } elseif ($Msg->value() == FALSE) {
        $error = TRUE;
    }
 
 
    if ($error === FALSE) {
        $Name = $Name->sanitize ('quote_smart');
        $Email = $Email->sanitize ('quote_smart');
        $Msg = $Msg->sanitize ('quote_smart');
 
        $SQL = 'INSERT INTO foo (name, email, message) VALUES (%s, %s, %s)';
        $SQL = sprintf ($SQL, $Name, $Email, $Msg); 
    }         
}
 
if ($error === TRUE) {
    // We have a situation.
}
code/tips.txt · Last modified: 2010/07/21 21:50 by davids
Back to top
CC Attribution-Share Alike 3.0 Unported
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0