tags:

views:

71

answers:

1

I have a form that validates the email address and I want to be able to place echo '<p class="error">Please enter a valid email address!</p>'; anywhere on the web page without having to put the validation process within the html?

Or should I include the validation process in the HTML form?

Here is the php code.

if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email'])) {
    $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email']));
} else {
    echo '<p class="error">Please enter a valid email address!</p>';
}
+2  A: 

Slightly different

if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email'])) { 
    $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email'])); 
} else { 
    $error = 'Please enter a valid email address!'; 
} 

Now you can print your $error anywhere on your page

Okay, make it this:

if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email'])) { 
    $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email'])); 
    echo "valid ";
} else { 
    $error = 'Please enter a valid email address!'; 
    echo "invalid ";
} 
echo $error;
exit;

What would it say now?

Another example:

<?php
$error="";
if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email'])) { 
  $email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email'])); 
} else { 
  $error = 'Please enter a valid email address!'; 
  $email=htmlspecialchars($email);
} 
?>
<html>
<form>
<?php if ($error): ?>
  <p class="error"><?php echo $error?></p>
<?php endif ?>
Enter email: <input type="text" name="email" value="<?php echo $email?>">
<input type="submit">
</form>
</html>

Now it works?

Col. Shrapnel
This did not work at all why?
GeNx
@GeNx: Then you just `echo $error;` wherever you want it to appear on your page. Did you do that?
animuson
@animuson of course I did that I'm not that much of a newbie :) I actually tried that before I posted this question.
GeNx
@GeNx may be you just entered valid email? or you trying to print it on some other page? or just before validation?
Col. Shrapnel
@Col. Shrapnel, No I left out the @ sign so I know its not valid.
GeNx
@GeNx **never** say "I know" but only "I have checked". Did you check what branch of your condition has worked? buy putting `echo "valid";` and `echo "invalid";` in the corresponding statements?
Col. Shrapnel
@Col. Shrapnel I didn't know I was in grammar school. And you lost me :(
GeNx
It's not grammar, it's common sense really. Are you trying to echo the variable in a function somewhere or is it in a function where it would not be accessible elsewhere?
animuson
@GeNx see addon to my post
Col. Shrapnel
@Col. Shrapnel, No I disagree with you and your common sense which is a little wacky:) As for your add on I get the following error. `valid Undefined variable: error`
GeNx
@GeNx so, you can disagree with me, but your code says your email passed validation and there is no error to show
Col. Shrapnel
@Col. Shrapnel, But I still can't place it any where on the web page :( Thanks though.
GeNx
@GeNx OMG **you can**! You can print any variable in any place! You just have to define it first! Thanks though.
Col. Shrapnel
@Col. Shrapnel OMG it wont work though when its directly in my HTML.
GeNx
@GeNx PHP can be used anywhere in HTML as long as it belongs to the same PHP script. Learn `<?php` tag
Col. Shrapnel
@Col. Shrapnel, HUH I did that but it did not work, hence the question:)
GeNx
@GeNx another edit
Col. Shrapnel