PDA

View Full Version : PHP Help to Validate Form DEADLINE



kwodlum
May 28th, 2008, 08:23 PM
I've done a search and I can't seem to find what I'm looking for, I have a deadline so I need help ASAP. Just help with one field to validate, than I can figure out the rest.

Here's the form: http://www.esl4work.com/form.html

Thanks in advance.




Here's the PHP code:



<?php




if(isset($_POST['submit'])) {




$firstName = $_POST['txtFirstName'];
$lastName = $_POST['txtLastName'];
$company = $_POST['txtCompany'];
$email = $_POST['txtEmail'];
$phone = $_POST['txtPhone'];
$hearAbout = $_POST['selHearAbout'];
$quest1 = $_POST['txtQuest1'];
$quest2 = $_POST['txtQuest2'];
$quest3 = $_POST['selQuest3'];
$quest4 = $_POST['txtOther'];
$quest5 = $_POST['txtQuest5'];

foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
}

$body = "From: $firstName $lastName\nE-Mail: $email\n\n1. What are the current positions of the participants? \n$quest1\n\n2. Approximately how many participants would be interested in talking an ESL course? \n$quest2\n\n3. What English Proficiency would you place the majority of the participants? \n$quest3\n\n4. Which areas of English do the participants need improvement in? \n$check_msg\nOther:\n$quest4\n\n5. What are your organizations top three goals for running an on-site ESL program? \n$quest5\n";

header ('location: http://www.esl4work.com/thank-you.html');
mail($to, $subject, $body);


}

else {

header ('location: http://www.esl4work.com/error.html');

}

?>

djheru
May 28th, 2008, 11:55 PM
Which field do you need to validate? Lets assume that it's the first name... First, I would put the form itself into a function so that you can reuse it if the validation fails. Just pass the form function an array that represents the $_POST array. Then echo the appropriate array key in the form field value attribute like so:


echo "<input type=\"text\" name=\"txtFirstName\" value=\"$postValues['txtFirstName']\" />";
Then, after submission:



if(isset($_POST['submit']))
{
//you could use regular expression matching for phone, email
if($_POST['txtFirstName'] != '')
{
$firstName = $_POST['txtFirstName'];
$lastName = $_POST['txtLastName'];
//etc...
}
else
{
echo "<h2>ERROR MESSAGE</h2>";
$form = formFunction($_POST);
}
}
else
{
echo "<h2>FILL OUT FORM</h2>";
$form = formFunction();
}


But I still like to use Javascript validation because it validates before the form is submitted to the server, so it is a little bit quicker