homepage Welcome to WebmasterWorld Guest from 54.167.173.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Form - Highlight invalid fields
wardy83




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

Hello!

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!

<?php
$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".
"Magazines:$sevena\n".
"Websites:$sevenb\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]

 

cameraman




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

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

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

<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']).

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved