text fields are easy enough to validate by using the empty() function.
file browsers are also easy to validate using the $_FILES['file']['error'] field.
but im having a little trouble dealing with null input from:
1. drop down menus
i have two drop down menus, the 2nd of which is populated with data when a selection is made from the 1st drop down menu. their respected first entries are "Please select". at the moment if nothing is selected (both drop down menus read "Please select") the string passed to my script is unmanagable. would setting the value of these 1st entries to "null" help me catch them?
2. multiple selections
at the moment i am getting around this problem softly by ensuring that when the page loads 1 of the items in the list is selected. however as the list is sent as an array to my script if no selections are mate the array is empty and an undefined index is thrown by the script. i have been looking for a way to catch and deal with this error but to no avail.
any tips?
validating form input
Moderator: General Moderators
Although checking that a field is not empty is not really validation... you need to verify that information is as you expect and if it is going into a database or displayed on the page, or emailed to you... you need to do validation specific to that respective purpose as well... here is something you could do...
Have you looked into HTML_QuickForm? It makes forms very very very easy. This form validates user input, displays errors back to the user in a very attractive fashion and sends an email to me when it is validated...
Code: Select all
<?php
$errors = array();
if(isset($_POST['form_sent'])){
if(empty($_POST['options'])) array_push($errors, 'options');
}
if(empty($errors)){
// Do stuff
echo 'YAY!';
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Validation</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form method="post" action="#">
<input type="hidden" name="form_sent" value="1">
<?php if(in_array('options', $errors)) echo 'Invalid Field Input<br>'; ?>
<select name='options[]' multiple="multiple">
<option value=''> - Select one or more - </option>
<option value='1'>One</option>
<option value='2'>Two</option>
<option value='3'>Three</option>
<option value='4'>Four</option>
<option value='5'>Five</option>
</select>
<br><input type="submit" value="Submit">
</form>
<?php
print_r($errors);
?>
</body>
</html>Code: Select all
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '/usr/share/pear');
require_once 'formValidator.php';
require_once 'Swift/Swift.php';
require_once 'Swift/Swift/Connection/SMTP.php';
$Form = new HTML_QuickForm('inquiries');
$Form->addElement('text', 'email', 'Email: ');
$Form->addElement('textarea', 'inquiry', 'Inquiry', 'rows="5" cols="55"');
$Form->addRule('inquiry', 'You did not enter an inquiry', 'required');
$Form->addRule('email', 'Email address required', 'required');
$Form->addRule('email', 'Email address must be valid', 'callback', array('formValidator', 'isEmail'));
$Form->addElement('submit', null, 'Send');
if($Form->validate()){
$values = $Form->getSubmitValues();
$email = "The following is an email generated by the Inquiry form at yoursite.com:\n\n";
foreach($values as $key => $value){
$email .= ucwords($key) . ": $value\n\n";
}
//Instantiate swift
$swift = new Swift(new Swift_Connection_SMTP('mail.yoursite.com'));
if ($swift->authenticate('username', 'password')){
//Send the email
if($swift->send(array('somebody@yoursite.com','dude@thebiglabowski.com'), $Form->exportValue('email'), 'Inquiry from yoursite.com', $email)){
$content = '<h1>Thank you,</h1><p>Your inquiry has been sent. We will get back to you as soon as possible.</p>';
}
else{
$content = "<h1>We're sorry, </h1><br><p>We were unable to send the inquiry. Please use the contact information to the left to contact us.</p>";
}
}
else{
$content = "<h1>We're sorry, </h1><br><p>We were unable to send the inquiry. Please use the contact information to the left to contact us.</p>";
}
$swift->close();
}
else{
$content = $Form->toHtml();
}
require 'template.tpl.php';
?>OK, well you should be able to customize that first script I posted to do just what you need... mess around with it a little bit... and then if you have questions, I'll be right here to ask.
Seriously though, when you get a chance, check out HTML_QuickForm. It takes a little bit to learn it and get used to it, but it's well worth it.
Seriously though, when you get a chance, check out HTML_QuickForm. It takes a little bit to learn it and get used to it, but it's well worth it.