Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Form - Highlight invalid fields

11:05 am on Nov 27, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 17, 2006
votes: 0


I am new to php but have designed a form in HTML which calls a php form for validation..... what I wanted to do is highlight the fields that are invalid...... at the moment it is calling headers which aren't able to indicate the fields that need to be filled in.

I have radio's, lists, checks and text boxes....... the php code is below....

all help greatly appreciated!

$sendto = "email@example.com";
$name =$_POST['name'];
$email =$_POST['email'];
$one =$_POST['one'];
$onespecify =$_POST['onespecify'];
$two =$_POST['two'];
$twospecify =$_POST['twospecify'];
$three =$_POST['three'];
$foura =$_POST['foura'];
$fourb =$_POST['fourb'];
$fourc =$_POST['fourc'];
$fourd =$_POST['fourd'];
$foure =$_POST['foure'];
$fourf =$_POST['fourf'];
$fourg =$_POST['fourg'];
$fourh =$_POST['fourh'];
$fourspecify =$_POST['fourspecify'];
$fivea =$_POST['fivea'];
$fiveb =$_POST['fiveb'];
$fivec =$_POST['fivec'];
$fived =$_POST['fived'];
$fivee =$_POST['fivee'];
$fivef =$_POST['fivef'];
$fivespecify =$_POST['fivespecify'];
$sixa =$_POST['sixa'];
$sixb =$_POST['sixb'];
$sixc =$_POST['sixc'];
$sixd =$_POST['sixd'];
$sixe =$_POST['sixe'];
$sixspecify =$_POST['sixspecify'];
$sevena =$_POST['sevena'];
$sevenb =$_POST['sevenb'];
$eight =$_POST['eight'];

$Logic1 = empty($name) empty($email) empty($one) empty($three) empty($sixa) empty($sixb) empty($sixc) empty($sixd);
$Logic2= ($sixe > 0) && empty($sixspecify);
$Logic3 = ($two=="Other") && empty($twospecify);
$Logic4 = ($fourh=="Other") && empty($fourspecify);
$Logic5 = ($fivef > 0) && empty($fivespecify);
$Logic6 = (($one=="Other") && empty($onespecify)) ($one=="Please select...");

if ($Logic1 $Logic2 $Logic3 $Logic4 $Logic5 $Logic6){
header ("Location: empty.html");
}elseif(!$email == "" && (!strstr($email,"@") !strstr($email,".")))
header ("Location: incorrect.html");

} else {
header ("Location: thankyou.html");

mail( "$sendto", "GCE Questionnaire",
"Name: $name\n\n".
"Email: $email\n\n".
"1. What industry do you work in?: $one $onespecify\n\n".
"2. What Industrial standards do you think are going to be the most important?: $two $twospecify\n\n".
"3. What is the typical port count you look for in a switch?: $three\n\n".
"4. What are the typical port types you use when connecting devices in a network?:\n\n".
"$foura, $fourb, $fourc, $fourd, $foure, $fourf, $fourg, $fourh, $fourspecify\n\n".
"5. Which of the following characteristics are important to you?: \n\n".
"$fivea, $fiveb, $fivec, $fived, $fivee, $fivespecify\n\n".
"6. Biggest factors in making a decision about device to purchase:\n\n".
"Length of warranty provided: $sixa\n".
"Technical Specifications: $sixb\n".
"Price: $sixc\n".
"Added services such as support or training: $sixd\n".
"$sixspecify: $sixe\n\n".
"7. What Magazines and websites to do with the market you operate in do you read?:\n\n".
"8. Do you belong to any professional bodies? : $eight");

[edited by: eelixduppy at 5:20 pm (utc) on Nov. 27, 2007]
[edit reason] exemplified email [/edit]

5:20 pm on Nov 27, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
votes: 0

Because php is server side, you get to decide exactly what is sent to the browser. As a simple example:
if($name == '')

<style type="text/css">
.req { background-color:yellow; }

<input type="text" <?php echo $bad_name;?> name="name" value="<?php echo $name;?>">

So the idea is to apply your validation rules and redisplay the form with the posted data - invalid inputs will have a yellow background and valid ones won't, and will retain their posted values (assuming $name comes from $_POST['name']).