0
votes

I was wondering how can I turn the echo part into a varialble (I think thats whats you call it) because I can post the "Please enter your last name!" any where on the web page.

// Check for a last name.
if (eregi ('^[[:alpha:]\.\' \-]{2,30}$', stripslashes(trim($_POST['last_name'])))) {
    $ln = escape_data($_POST['last_name']);
} else {
    $ln = FALSE;
    echo '

Please enter your last name!

'; }
3

3 Answers

6
votes

You could gather the errors in an array and print them later:

$errors = array();

// Check for a last name.
if (eregi ('^[[:alpha:]\.\' \-]{2,30}$', stripslashes(trim($_POST['last_name'])))) {
    $ln = escape_data($_POST['last_name']);
} else {
    $ln = FALSE;
    $errors[] = 'Please enter your last name!'
}

if ($errors) {
    echo '<ul>';
    echo '<li>'.implode('</li><li>', $errors).'</li>';
    echo '</ul>';
} else {
    // no errors occured
}
0
votes

If all you want to do is store that one string in a variable and re-use it, it would be something like this (may not be correct php)

$errorMsg = 'Please enter your last name!';

then wherever you want to use it

echo $errorMsg;

but I recommend doing what the above person said if you have more than one string you wish to re-use.

0
votes

eregi is DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0.

I would recommend to stop using it and use something like preg_match in addition to Gumbo's answer, just change out the use of the eregi function for the preg family