I have a rough idea of what I am trying to produce - Contact Processing Script.
What I am stuck on is how do I make the PHP process the script if all the javascript values are correct else do nothing due to the values not being correct. What do I need to set within the isset?
PHP will have no idea what the JavaScript did, unless you somehow include the JavaScript results in the post/get data that is sent to the PHP script. (And note that JavaScript can be easily bypassed, either inadvertently by users with it disabled or maliciously by robot scripts or such, so you really need to do any important validation in the PHP script, too.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
PHP will have no idea what the JavaScript did, unless you somehow include the JavaScript results in the post/get data that is sent to the PHP script. (And note that JavaScript can be easily bypassed, either inadvertently by users with it disabled or maliciously by robot scripts or such, so you really need to do any important validation in the PHP script, too.)
Yep thats what is getting me confused. I want the functional checking like JS in PHP but I no it cannot be done and I cannot find a good example of a PHP contact form. Any Ideas?
A fairly simple, all-purpose approach is something like:
PHP Code:
$errors = array();
if(empty($_POST['name'])) {
$errors[] = "You must enter a Name.";
}
if(empty($_POST['email']) or filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == false) {
$errors[] = "Please enter a valid email address.";
}
if(count($errors)) { // we have at least one error
echo "<p class='error'>" . implode("<br />\n", $errors) . "</p>\n";
}
else {
// go ahead and process the form data here
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Instead of having a if statement for every field in the form could I have it like this? My concept in my head is fine but I am having an issue to explain it.
PHP Code:
$values = new array('[B]gets data from the $_POST[/B]'); // I am unsure how I would do this? I have used php for years but this has been one of my biggest confusions
I don't know: probably, but it's a built-in function. It just makes sure it's reasonably valid syntax, but does not rigidly enforce the standards.
Could I use Trim in this statement if so were?
Sure. I often apply trim() to the $_POST array separately via array_walk_recursive() before doing anything else with it.
Is captcha a good idea?
Only if you think you really need it, as users find it annoying.
How would I impliment a required section?
Not sure what you mean, but I think that's what the empty() check would be fore. If you want to enforce minimum or maximum numbers of characters, you can use strlen() to check the length, or substr() to just truncate any extra characters if that's desired.
Instead of having a if statement for every field in the form could I have it like this? My concept in my head is fine but I am having an issue to explain it.
PHP Code:
$values = new array('[B]gets data from the $_POST[/B]'); // I am unsure how I would do this? I have used php for years but this has been one of my biggest confusions
[/quote]
You could set up some sort of array defining field names, types, minimum/maximum lengths, and so forth, then loop through that array to run a generic validation function on each.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Bookmarks