3

I want to make a little contact form, where the user inputs his name, email, and the message. The form is submitted with ajax to a php file that has to do the following:

1- check if the 3 posted variables exist and not NULL 2- sanitize and verify the 3 variables for malicious code and some criteria, like the name must be and the email must be.. 3- send the data with php's mail().

how can I achieve the first and the second steps with php filter

NB: I took a look at the php's manual and I understood nothing.

Thanks.

1
  • Keep in mind that an email submitted this way may have valid syntax, but not be correct or belong to the user. Especially don't mail() anything directly to it – that's an easy way to get into spam blacklists. Commented Feb 15, 2011 at 21:07

2 Answers 2

3

1. Use isset() or array_key_exists() on $_POST to see if values exist.

if (isset($_POST['a_field']))

// or

if (array_key_exists('a_field', $_POST))

You can also use filter_has_var, but it's also got a "gotcha" that you need to be aware of. It does not work off of PHP's superglobals, instead relying on the data that's sent to PHP. If you manually declare something in your script, e.g. $_POST['test'], filter_has_var will not see it.

How to use filter_has_var:

if (filter_has_var(INPUT_POST, 'test'))



2. Do you want to sanitize data or validate it? (two different things).

Assert that name and email have values, and that email is a valid email:

if (!empty($_POST['name']))

if (!empty($_POST['email']) && filter_input(INPUST_POST, 'email', FILTER_VALIDATE_EMAIL))
Sign up to request clarification or add additional context in comments.

2 Comments

you can just use !empty as it performs an isset check internally as well.
thanks for providing the array_key_exists method - i needed that for another project :-)
1

you can do this for email:

filter_var('[email protected]', FILTER_VALIDATE_EMAIL)

Returns the filtered data, or FALSE if the filter fails.

you can for just validate the value do this with array.

$args = array(
    'name'   => array('filter'    => FILTER_VALIDATE_BOOLEAN,
                            'flags'     => FILTER_NULL_ON_FAILURE),
    'email'    => array('filter'    => FILTER_VALIDATE_BOOLEAN,
                            'flags'     => FILTER_NULL_ON_FAILURE),
     'message'    => array('filter'    => FILTER_VALIDATE_BOOLEAN,
                            'flags'     => FILTER_NULL_ON_FAILURE));

$myinputs = filter_input_array(INPUT_GET, $args);

you can add multi filter or multi flag to one field like this

email => array("filter" => array(FILTER_VALIDATE_EMAIL ,FILTER_VALIDATE_BOOLE)

1 Comment

you can add more option for each filed or change the filter for email to FILTER_VALIDATE_EMAIL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.