Hi all,
I suspect that there may be a sample solution for my problem but after a week of trying various solutions I am still stumped.
The code blow validates the two variables. This works if i do not send the details to form.php, so the line: <form name="form" action="" method="POST"> will validate the data and work fine.
However if i want to send the details to form.php to send to my email: <form name="form" action="form.php" method="POST">. the validation stops working and I can put anything on the form and it arrives in my email with no validation
Why would this be?
Both coding below. this is validation code
<?php
$user = ['name' => '', 'age' => '', 'terms' => '', ];
$errors = ['name' => '', 'age' => '', 'terms' => false, ];
$message = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') { //if from submitted
// validation filters
$validation_filters['name']['filter'] = FILTER_VALIDATE_REGEXP;
$validation_filters['name']['options']['regexp'] = '/^[A-z]{2,10}$/';
$validation_filters['age']['filter'] = FILTER_VALIDATE_INT;
$validation_filters['age']['options']['min_range'] = 16;
$validation_filters['age']['options']['max_range'] = 65;
$user = filter_input_array(INPUT_POST, $validation_filters); // validate data
//create error messages
$errors['name'] = $user['name'] ? '' : 'Name must be 2-10 letters using A-z';
$errors['age'] = $user['age'] ? '' : 'You must be 16-65';
$invalid = implode($errors);
if ($invalid) {
$message = 'Please correct the following errors: ';
} else {
$message = 'Thank you, your data is valid';
}
//Sanitize Data
$user['name'] = filter_var($user['name'], FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$user['age'] = filter_var($user['age'], FILTER_SANITIZE_NUMBER_INT);
}
?>
<?php include 'includes/header.php'; ?>
<?= $message ?>
<form name="form" action="form.php" method="POST">
Name: <input type="text" name="name" value="<?= $user['name'] ?>">
<span class="error"><?= $errors['name'] ?></span>
Age: <input type="text" name="age" value="<?= $user['age'] ?>">
<span class="error"><?= $errors['age'] ?></span><br>
<input type="submit" value="Submit">
</form>
.
This is form.php
<?php
$name = $_POST['name'];
$age = $_POST['age'];
$to = 'myemail.com';
$subject = 'Contact';
$msg = "Name: $name\n" .
"Age: $age\n";
mail($to, $subject, $msg, 'from:' . myemail.com);
?>
<br />
<?php
echo 'Thanks ' . $name . ' We have your info!<br />';
echo "Details: <br />";
echo 'name: ' . $name . '<br />';
echo 'age: ' . $age . '<br />';
?>
<a href="validate-form-using-filters.php" > GO back to form</a>
Apologies for all the code but at this point in time I think there must be something other than form action at play here. All help apricated.