Jump to content

Help with FILTER_SANITIZE_EMAIL


leeh14

Recommended Posts

Hi,

 

I am building a website and need all the fields to be sanitized and validated. When running a filter over a standard string I have a count function to detect when the function was successfull and if all feilds are successful the page can move on. However i hit issues when i reach a e-mail function. Basically i can't get the count to run only if the e-mail is vaild? so basically where can the count be placed to run only if the string is valid and after it has been sanitized? 

 

Function:

 

        if ($_POST['email'] != "") {  
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);  
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {  
                $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";  
            }  
        } else {  
            $errors .= 'Please enter your email address.<br/>';  
        }  
  

 

Count:

 

$count ++

 

any help on this matter would be great!

 

Lee

Link to comment
Share on other sites

As an else to the if( !filter_var( . . .) ) { conditional, if tI understand correctly what you're trying to do. However, it's usually easier to store error messages in an array, then, if( count($array) === 0 ), there are no errors. And if there are errors, you can display them all with a foreach() loop.

 

$errors = '';
         if ($_POST['email'] != "") {
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
            if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $errors .= "$email is <strong>NOT</strong> a valid email address.<br/><br/>";
            } else {
            	$count++;
            }
        } else {
            $errors .= 'Please enter your email address.<br/>';
        }

Link to comment
Share on other sites

That's brill has saved my project! One more thing before I set this thread to solved!

 

Once the functions have run if you refresh the page a message comes up and says do you want to run the functions again! if you click yes it adds the data to the database again! How can i stop this from happening?

 

 

Link to comment
Share on other sites

OK to stop this from happening i have moved the php to a separate file which is run when submitted, which works fine for submitting to the database. But I have some validation in there and the user might need to amend fields. So how can I post any error message back to the form along with what the user typed in? 

Link to comment
Share on other sites

This is a stripped out example of the logic that you could use. The principle is the same, but you'd have more fields, and validation routines, etc.

 

process.php

<?php
if( isset($_POST['submit']) ) {
if( empty($_POST['name']) ) {
	$errors[] = 'Name is empty.';
}
if( empty($errors) ) {
	// insert data
	// if insert is ok, either redirect, or display success message.
} else {
	$i = 1;
	$count = count($errors);
	foreach( $errors as $v ) {
		echo "<font color=\"red\">$v</font>";
		echo $i < $count ? "<br>" : '';
		$i++;
	}
	include('form.php');
}
} else {
include('form.php');
}
?>

 

form.php

<form action="process.php" method="post">
<input type="text" name="name" value="<?php echo isset($_POST['name']) ? "{$_POST['name']}" : ''; ?>">
<input type="submit" name="submit" value="Submit">
</form>

Link to comment
Share on other sites

Hi,

 

Cheers i will give that a try! Just seems annoying all i want to do is hit submit run php that check if there are errors and if there are errors display them. And if there are no errors run anouther php to add all the field to the database then redirect back to the form page. Seems simple but is not!

 

Regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.