PHP Form Validation ???

This is a discussion on "PHP Form Validation ???" within the PHP Forum section. This forum, and the thread "PHP Form Validation ??? are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack (1) Thread Tools
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old May 25th, 2006, 11:08
Junior Member
Join Date: Apr 2006
Age: 27
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Question PHP Form Validation ???

I wonder if anyone around could possibly enlighten me a little (or indeed, a LOT) towards "Contact Us" form validation in PHP.

Basically, I've created the form & then typed in the validation related codes from 3 different books along with differerent websites so far, but ONLY, if I understood it.

However, there are only couple things that's beyond me, which can be seen from the comment around the code that I didn't understand i.e. comment typed after "//"

Much of the code is still missing bearing in mind my expectation from this form and my lack of knowledge in how to go about doing it.

This is what I'm aiming for:

>> Users MUST fill in all the fields (including radio button field and drop-down menu field)
>> If they don't then they should get a message saying so
>> If they don't fill any certain field then they should get a message saying "Please fill in your E-mail" (for example) OR if they don't select appropriate "radio button" option or "drop-down" menu option then they should get an error message saying "please select the appropriate "Title" from the drop-down list", etc. OR "please choose the means of contacting you" (by choosing appropriate radio button)
>> The "blanked" or "incorrect data entry" field should turn to "red" making it easier for user to realise what's missing

>> After user fills everything & when they click "Preview" they should get some sort of "message" (ONLY if the data is valid and ALL required fields filled) saying "Following are the details you filled in" and then underneath it, CONFIRM their title, name, e-mail, telephone number, message, ways of contacting back to them, etc. (basically, ALL the field) with "Submit" and "Edit" button. Selecting "Edit" will take them back to the form, with all the data they filled in STILL there (rather than making the re-type everything)
>> If they simply click on submit (after filling the required & valid data) then they should get a message saying "Thanks for your message, we'll be in touch shortly".

The codes that I've created as yet are as follows:

Code: Select all
<?php include("menu.inc");?>

<div id="centerContent">

<p class="first-letter">Please fill the following form in for any enquiries that you may have:</p>
<p>

<?php

// if the form has been posted, analyse it: 
if ($_POST) {
foreach ($_POST as $field => $value) {
$value = trim($value);
}

// creating variables
$inquiry=$_POST['inquiry'];
$title=$_POST['title'];
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
$reply=$_POST['reply'];
$contact=$_POST['contact'];

// create empty error variable
$error = ""; 

// check for data in required fields
if (($inquiry == "") || ($title == "") || ($first_name == "") || ($last_name == "") || ($email == "") || ($phone == "") || ($message == "") || ($reply == "") || ($contact == "")) {
$error = "Please fill in all the required fields!";
}

// validate first_name
if ((ctype_alpha($first_name) == FALSE 
    $error = "Please enter a valid name (Alphabets only)<br/>";
}

// validate surname
if ((ctype_alpha($last_name) == FALSE {
    $error = "Please enter a valid last name (Alphabets only)<br/>";
}

// validate e-mail
if ((strpos($email, "@") === FALSE || 
    (strpos($email, ".") === FALSE ||
    (strpos($email, " ") != FALSE || // DON'T KNOW WHY USING "!=" SIGN
    (strpos($email, "@") === FALSE || // DON'T KNOW WHY USING 3 "=" SIGN
    (strpos($email, "@") > strrpos($email, "."))) {
    $error = "Please enter a valid e-mail address<br/>";
}

// validate phone
if (is_numeric($phone) == FALSE {
    $error = "Please enter a valid contact number (must contain numbers only)<br/>";
}

//If everything is OK then Confirmation of each field's data as filled by the user
else {
echo "<p><b>Nature of Inquiry:</b> $inquiry<br/>";
echo "<p><b>Title:</b> $title<br/>";
echo "<p><b>First Name:</b> $first_name<br/>";
echo "<p><b>Last Name:</b> $last_name<br/>";
echo "<p><b>E-mail:</b> $email<br/>";
echo "<p><b>Contact No.:</b> $phone<br/>";
echo "<p><b>Message:</b> $message<br/>";
echo "<p><b>Reply:</b> $reply<br/>";
echo "<p><b>Contact Method:</b> $contact<br/>";

echo "Thanks for contacting us. We will get back to you shortly!";
}
}
?>
</p>

<table class="table">

<tr class="tr">
<td class="td"><form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Nature of Enquiry:</td>
<td class="td"><select name="inquiry" value="<?php echo $inquiry ?>">
<option></option>
<option>General Inquiry</option>
<option>Price Quotation</option>
<option>Comments</option>
<option>Other</option>
</select></td>
</tr>

<tr class="tr">
<td class="td">Title:</td>
<td class="td"><select name="title" value="<?php echo $title ?>">
<option></option>
<option>Mr</option>
<option>Miss</option>
<option>Mrs</option>
<option>Other</option>
</select></td>
</tr>

<tr class="tr">
<td class="td">First Name:</td>
<td class="td"><input type="text" size="30" maxlength="30" name="first_name" value="<?php echo $first_name ?>"/></td>
</tr>

<tr class="tr">
<td class="td">Last Name:</td>
<td class="td"><input type="text" size="30" maxlength="30" name="last_name" value="<?php echo $last_name ?>"/></td>
</tr>

<tr class="tr">
<td class="td">E-mail:</td>
<td class="td"><input type="text" size="30" maxlength="30" name="email" value="<?php echo $email ?>"/></td>
</tr>

<tr class="tr">
<td class="td">Contact No.:</td>
<td class="td"><input type="text" size="30" maxlength="20" name="phone" value="<?php echo $phone ?>"/></td>
</tr>

<tr class="tr">
<td class="td">Message:</td>
<td class="td"><textarea rows="10" cols="50" wrap="physical" name="message" value="<?php echo $message ?>">
</textarea></td>
</tr>

<tr class="tr">
<td class="td">Reply Required:</td>
<td class="td"><input type="radio" name="reply" value="<?php echo $reply ?>"/>Yes
<input name="reply" type="radio"/>No
<input name="reply" type="radio"/>Maybe</td>
</tr>

<tr class="tr">
<td class="td">How would you like to be contacted (if required)?<br/><br/></td>
<td class="td"><input type="radio" name="contact" value="<?php echo $contact ?>"/>E-mail
<input type="radio" name="contact"/>Telephone</td>
</tr>

<tr class="tr">
<td class="td"></td>
<td class="td"><input type="reset" name="reset" value="Reset"/>
<input type="preview" name="preview" value="Preview"/></td>
<input type="submit" name="submit" value="Submit"/></td>
</form>
</tr>
</table>
</div>
Reply With Quote

  #2 (permalink)  
Old May 28th, 2006, 06:30
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Form Validation ???

James, you're headed down a coding style that's going to give you an awful lot of code that will be hard to enhance and maintain in the future; I would point you towards Rasmus Lerdorf's 4 layer model and a use of functions to generate each row of your form. The "stickyness" of the incorrect / previous entries, and validation, all handled in write-once functions.

Links:
Source code
Related articles and included files
Try out the code
Learn more about it

-- Graham
Reply With Quote
  #3 (permalink)  
Old May 31st, 2006, 23:08
New Member
Join Date: May 2006
Location: Utah
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to agent-j Send a message via MSN to agent-j Send a message via Yahoo to agent-j
Re: PHP Form Validation ???

You might also want to look at what the HTML QuickForm package that comes with PEAR can do. It will do both server and client side validation for you.

http://www.phpfreaks.com/pear_manual...quickform.html
Reply With Quote
Reply

Tags
php, form, validation

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.webforumz.com/php-forum/6522-php-form-validation.htm
Posted By For Type Date
Incorrect data in PHP form? This thread Refback Mar 28th, 2008 15:28

Similar Threads
Thread Thread Starter Forum Replies Last Post
Form validation help! psycho wolvesbane JavaScript Forum 16 Feb 12th, 2008 16:40
AS Form / Validation Help papalazarou78 Flash & Multimedia Forum 0 Jul 31st, 2007 19:43
Form Validation cjrollo Flash & Multimedia Forum 0 Feb 22nd, 2007 17:33
Form Validation feebee JavaScript Forum 1 Aug 3rd, 2006 16:12
PHP Form Validation. kaz PHP Forum 2 Jul 22nd, 2006 20:47


All times are GMT. The time now is 22:07.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43