Form Error Styling

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
obi_wasabi
Forum Newbie
Posts: 15
Joined: Thu Dec 16, 2004 12:30 pm
Location: CA

Form Error Styling

Post by obi_wasabi »

Hi,


I've come up with a solution that would solve my form error problem, see previous post "Customizing Mail Form Errors." Being new to PHP, I need assitance with implementation:

I want the form submission pointing back to the index page. If there are errors, display different versions of the label (ie <input type="text" name="first_name" class="Missing" /> or <p class="Missing">First Name</p>).

Then I can design my CSS so that the labels and/or input fields change in appearance to indicate the missing elements.

http://www.ckimedia.com/php_version/index.php

Assistance would be appreciated.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Not sure I'm understanding you entirely, in what you know and do not know, but I'm giving it a try;

If $_POST['somefield'] is missing a value (or have the wrong one entered) add it do a new variable. Check against that later to display any errors. Example;

Code: Select all

if (empty($_POST['foo'])) {
 $bad['foo'] = 'Foo cannot be empty!';
}
 // ... etc ...

if (!empty($bad)) {
    foreach ($bad as $k => $v) {
        echo '<span id="error">'.$k.' : '.$v.'</span>';
    }
}
Should get you going some more.
obi_wasabi
Forum Newbie
Posts: 15
Joined: Thu Dec 16, 2004 12:30 pm
Location: CA

Post by obi_wasabi »

JAM wrote:Not sure I'm understanding you entirely, in what you know and do not know, but I'm giving it a try;

If $_POST['somefield'] is missing a value (or have the wrong one entered) add it do a new variable. Check against that later to display any errors. Example;

Code: Select all

if (empty($_POST['foo'])) {
 $bad['foo'] = 'Foo cannot be empty!';
}
 // ... etc ...

if (!empty($bad)) {
    foreach ($bad as $k => $v) {
        echo '<span id="error">'.$k.' : '.$v.'</span>';
    }
}
Should get you going some more.

Hi,

Thanks for your reply, below is the form_handdling code I have written, I pretty happy with. I need this page to point back to the index page with
different versions of the label (ie <input type="text" name="first_name" class="Missing" /> or <p class="Missing">First Name</p>). That i'll control with the css within the index page.

Also is it possible to call a variable inside of the $header variable?

Code: Select all

$headers="From: $from\n";
$from='someone@somewhere.com';
Sorry If my first post was vauge

Code: Select all

<?php #BushidoDeep script - handle_form.php
$mail_to="nomail@nomail.com";
$subject="Web Mail Form Reply";
$date = date("F j, Y, g:i a"); 
$headers = "From: webmaster@bushidodeep.com\n";








if(strlen($fm_firstname)>0){
		$fm_firstname=stripslashes($fm_firstname);
	}else{//If no name was entered.
		$fm_firstname=NULL;
		echo '<p><em>You forgot to enter your first name!</em></p>';
	}	
if(strlen($fm_lastname)>0){
		$fm_lastname=stripslashes($fm_lastname);
	}else{//If no name was entered.
		$fm_lastname=NULL;
		echo '<p><em>You forgot to enter  your last name!</em></p>';
}
if(isset($fm_newsopt)){
	if($fm_newsopt=='yes'){
	echo '<p><strong>Additional BushidoNews forthcoming.</strong></p>';
	}elseif($fm_newsopt=='no'){
	echo '<p><strong>No News For You.</strong></p>';
	}
	}else{
	$fm_newsopt=NULL;
	echo '<p><strong>Would You like additional news?</strong></p>';
	}


$email=$fm_email;	
if(!validate_email($email)){
	echo '<p><strong>Your Email is Invalid</strong></p>';
	}
$zipcode=$fm_zipcode;
if(!validate_zipCode($zipcode)){
	echo '<p><strong>Your Zipcode is Invalid.</stong></p>';
}	
if($fm_firstname && $fm_lastname && $fm_zipcode && $fm_email && $fm_newsopt){// if all is well
	
	//send form data.
	$body= "$date this sumbmission was sent from the BushidoDeep web mail form: \n From: \n First Name: {$_POST['fm_firstname']}\n Last Name: {$_POST['fm_lastname']}\n E-Mail: '{$_POST['fm_email']}'\n ZipCode: {$_POST['fm_zipcode']}\n Comments:  {$_POST['fm_comments']}";
	mail($mail_to,$subject,$body,$headers);
	//dislay confirmation
	echo "Thank you {$_POST['fm_firstname']} for visiting bushidoDeep,<br />Your email is '{$_POST['fm_email']}' and your zip code is '{$_POST['fm_zipcode']}'.";
	}else{
		echo '<p>Please Try Again!</p>';
	}	
function validate_email($email){
	//Create the syntactical validation regular expressions.
	$regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
	//presume email is invalid
	$valid=0;
	//validate the syntax
	if(eregi($regexp, $email))
	{
		list($username,$domaintld)=split("@",$email);
		//validate the domain
	if(getmxrr($domaintld,$mxrecords))
	$valid=1;
	}else{
	$valid=0;
	}
	return $valid;
	}	
	
function validate_zipCode($zipcode){
	$pattern = "/^([0-9]{5})([\-]{1}[0-9]{4})?$/";
if (preg_match($pattern,$zipcode)) {
	$valid =1;
	}else{
	$valid = 0;
	}
	return $valid;
}




?>
Post Reply