Jump to content

Vallidation fail = fields blanked...


nimzie

Recommended Posts

Hi,

 

I'm using the validation_class.php class to validate forms. If the validation succeeds, I use the Header function to redirect to the next page. If it fails, I put up the error messages as I'd hoped to with this class, but all of the values in the field would ideally remain.

What is a good method for preserving things?

 

Here is an example of my code:

 

<?php 
require "php/validation_class.php";

if (isset($_POST['submit'])) {
$example = new Validate_fields;
$example->check_4html = false;
$example->add_link_field("Email_address", $_POST['email'], "email");	
$example->add_text_field("Simple_text", $_POST['code'], "text", "y");	
if ($example->validation()) {
            header(
              "location: contest.php?&email={$_POST['email']}&code={$_POST['code']}");
} else {
    $error = $example->create_msg();
}
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>No More Secrets</title>
<link href="styles.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"  method="post">
<div id="wrapper">
<div id="headerbar"><div id="header"><a href="/"><img src="images/NMS_banner.gif" alt="bla bla" width="960" height="200" border="0" /></a></div>
</div>
<div id="navigation"><div id="navigationinside">
<ul id="menu">
<li><a href="index.php" id="selected"><strong>Home </strong></a></li>
<li><a href="secrets.html">More blah</a></li>
<li><a href="prizes/index.php">Prizes</a></li>
<li><a href="http://www.nautel.com/contact.aspx">Contact Us </a></li>
<li><a href="http://www.___________.com">Visit ________.com </a></li>
</ul></div>
</div>
<div id="content_home">
  <div id="maincontent">
    <script type="text/javascript">
AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','width','644','height','441','title','Flash','src','digital','quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','movie','digital' ); //end AC code
</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="644" height="441" title="Flash">
      <param name="movie" value="digital.swf" />
      <param name="quality" value="high" />
      <embed src="digital.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="644" height="441"></embed>
    </object></noscript></div>
  <div id="sidebar">
  
  <h1>Enter Contest</h1>
  <h3>Enter Code</h3> 
  <input type="text" name="code" /><br/>
  <h3>Enter email address</h3>
  <input type="text" name="email" /><br/>
  <input type="submit" name="submit" value="Enter" />

  <h3>Win a <a href="prizes/index.php">This is the prize</a>!</h3>
  <p style="color:#FF0000;padding-left:65px;">
  <?php 
    if (isset($error)) 
  echo "{$error}"; 
  ?>
  </p>
</div>
<div class="clear"></div></div>
<div id="mdrbkgd">
</div>
</form>
</body>
</html>

 

Link to comment
https://forums.phpfreaks.com/topic/70612-vallidation-fail-fields-blanked/
Share on other sites

This is all the code in the library. I left some of the legal jargon on top cause Olaf Lederer wrote it and I just customized the languages to be en/fr only. That's the only change I've made.

I'm off for a bit however will try the suggested  value=$_POST['Field'] as part of the HTML code to build the controls....

 

Thanks for the help!

 

<?php 
/************************************************************************
Validate_fields Class - verion 1.35
Easy to use form field validation

Copyright (c) 2004 - 2006, Olaf Lederer
All rights reserved.
...
...
...
______________________________________________________________________
available at http://www.finalwebsites.com 
Comments & suggestions: http://www.finalwebsites.com/contact.php

*************************************************************************/

//error_reporting(E_ALL);

class Validate_fields {

var $fields = array();
var $messages = array();
var $check_4html = false;
var $language;

function Validate_fields() {
	$this->language = "en";
	$this->create_msg();
}
function validation() {
	$status = 0; 
	foreach ($this->fields as $key => $val) {
		switch ($val['type']) {
			case "email":
			if (!$this->check_email($val['value'], $key, $val['required'])) {
				$status++;
			}
			break;
			case "number":
			if (!$this->check_num_val($val['value'], $key, $val['length'], $val['required'])) {
				$status++;
			}
			break;
			case "decimal":
			if (!$this->check_decimal($val['value'], $key, $val['decimals'], $val['required'])) {
				$status++;
			}
			break;
			case "date":
			if (!$this->check_date($val['value'], $key, $val['version'], $val['required'])) {
				$status++;
			}
			break;
			case "url":
			if (!$this->check_url($val['value'], $key, $val['required'])) {
				$status++;
			}
			break;
			case "text":
			if (!$this->check_text($val['value'], $key, $val['length'], $val['required'])) {
				$status++;
			}
			break;
			case "checkbox":
			case "radio":
			if (!$this->check_check_box($val['value'], $key, $val['element'])) {
				$status++;
			}
		} 
		if ($this->check_4html) {
			if (!$this->check_html_tags($val['value'], $key)) {
				$status++;
			}
		}
	}
	if ($status == 0) {
		return true;
	} else {
		$this->messages[] = $this->error_text(0);
		return false;
	}
}
function add_text_field($name, $val, $type = "text", $required = "y", $length = 0) {
	$this->fields[$name]['value'] = $val;
	$this->fields[$name]['type'] = $type;
	$this->fields[$name]['required'] = $required;
	$this->fields[$name]['length'] = $length;
}
function add_num_field($name, $val, $type = "number", $required = "y", $decimals = 0, $length = 0) {
	$this->fields[$name]['value'] = $val;
	$this->fields[$name]['type'] = $type;
	$this->fields[$name]['required'] = $required;
	$this->fields[$name]['decimals'] = $decimals;
	$this->fields[$name]['length'] = $length;
}
function add_link_field($name, $val, $type = "email", $required = "y") {
	$this->fields[$name]['value'] = $val;
	$this->fields[$name]['type'] = $type;
	$this->fields[$name]['required'] = $required;
}
function add_date_field($name, $val, $type = "date", $version = "us", $required = "y") {
	$this->fields[$name]['value'] = $val;
	$this->fields[$name]['type'] = $type;
	$this->fields[$name]['version'] = $version;
	$this->fields[$name]['required'] = $required;
}
function add_check_box($name, $element_name, $type = "checkbox", $required_value = "") {
	$this->fields[$name]['value'] = $required_value;
	$this->fields[$name]['type'] = $type;
	$this->fields[$name]['element'] = $element_name;
}
function check_url($url_val, $field, $req = "y") {
	if ($url_val == "") {
		if ($req == "y") {
			$this->messages[] = $this->error_text(1, $field);
			return false;
		} else {
			return true;
		}
	} else {
		$url_pattern = "http\:\/\/[[:alnum:]\-\.]+(\.[[:alpha:]]{2,4})+";
		$url_pattern .= "(\/[\w\-]+)*"; // folders like /val_1/45/
		$url_pattern .= "((\/[\w\-\.]+\.[[:alnum:]]{2,4})?"; // filename like index.html
		$url_pattern .= "|"; // end with filename or ?
		$url_pattern .= "\/?)"; // trailing slash or not
		$error_count = 0;
		if (strpos($url_val, "?")) {
			$url_parts = explode("?", $url_val);
			if (!preg_match("/^".$url_pattern."$/", $url_parts[0])) {
				$error_count++;
			}
			if (!preg_match("/^(&?[\w\-]+=\w*)+$/", $url_parts[1])) {
				$error_count++;
			}
		} else {
			if (!preg_match("/^".$url_pattern."$/", $url_val)) {
				$error_count++;
			}
		}
		if ($error_count > 0) {
			$this->messages[] = $this->error_text(14, $field);
				return false;
		} else {
			return true;
		}
	}
}
function check_num_val($num_val, $field, $num_len = 0, $req = "n") {
	if ($num_val == "") {
		if ($req == "y") {
			$this->messages[] = $this->error_text(1, $field);
			return false;
		} else {
			return true;
		}
	} else {
		$pattern = ($num_len == 0) ? "/^\-?[0-9]*$/" : "/^\-?[0-9]{0,".$num_len."}$/";
		if (preg_match($pattern, $num_val)) {
			return true;
		} else {
			$this->messages[] = $this->error_text(12, $field);
			return false;
		}
	}
}
function check_text($text_val, $field, $text_len = 0, $req = "y") {
	if (empty($text_val)) {
		if ($req == "y") {
			$this->messages[] = $this->error_text(1, $field);
			return false;
		} else {
			return true; // in case only the text length is validated
		}
	} else {
		if ($text_len > 0) {
			if (strlen($text_val) > $text_len) {
				$this->messages[] = $this->error_text(13, $field);
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}
}
function check_check_box($req_value, $field, $element) {
	if (empty($_REQUEST[$element])) {
		$this->messages[] = $this->error_text(12, $field);
		return false;
	} else {
		if (!empty($req_value)) {
			if ($req_value != $_REQUEST[$element]) {
				$this->messages[] = $this->error_text(12, $field);
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}
}
function check_decimal($dec_val, $field, $decimals = 2, $req = "n") {
	if ($dec_val == "") {
		if ($req == "y") {
			$this->messages[] = $this->error_text(1, $field);
			return false;
		} else {
			return true;
		}
	} else {
		$pattern = "/^[-]*[0-9][0-9]*\.[0-9]{".$decimals."}$/";
		if (preg_match($pattern, $dec_val)) {
			return true;
		} else {
			$this->messages[] = $this->error_text(12, $field);
			return false;
		}
	}
}
function check_date($date, $field, $version = "us", $req = "n") { 
	if ($date == "") {
		if ($req == "y") {
			$this->messages[] = $this->error_text(1, $field);
			return false;
		} else {
			return true;
		}
	} else {
		$date_parts = explode("-", $date);
		$month = $date_parts[1];
		if ($version == "eu") {
			$pattern = "/^(0?[1-9]|[1-2][0-9]|3[0-1])[-](0?[1-9]|1[0-2])[-](19|20)[0-9]{2}$/";
			$day = $date_parts[0];
			$year = $date_parts[2];
		} else {
			$pattern = "/^(19|20)[0-9]{2}[-](0?[1-9]|1[0-2])[-](0?[1-9]|[1-2][0-9]|3[0-1])$/";
			$day = $date_parts[2];
			$year = $date_parts[0];
		}
		if (preg_match($pattern, $date) && checkdate(intval($month), intval($day), $year)) {
			return true;
		} else {
			$this->messages[] = $this->error_text(10, $field);
			return false;
		}
	}
}
function check_email($mail_address, $field, $req = "y") {
	if ($mail_address == "") {
		if ($req == "y") {
			$this->messages[] = $this->error_text(1, $field);
			return false;
		} else {
			return true;
		}
	} else {
		if (preg_match("/^[0-9a-z]+(([\.\-_])[0-9a-z]+)*@[0-9a-z]+(([\.\-])[0-9a-z-]+)*\.[a-z]{2,4}$/i", $mail_address)) {
			return true;
		} else {
			$this->messages[] = $this->error_text(11, $field);
			return false;
		}
	}
}
function check_html_tags($value, $field) {
	if (preg_match("/<[a-z]+(\s[a-z]{2,}=['\"]?(.*)['\"]?)+(\s?\/)?>(<\/[a-z]>)?/i", $value)) {
		$this->messages[] = $this->error_text(15, $field);
		return false;
	} else {
		return true;
	}
}
function create_msg($break_elem = "<br />") {
	$the_msg = "";
	krsort($this->messages); // modified in 1.35
	reset($this->messages); 
	foreach ($this->messages as $value) {
		$the_msg .= $value.$break_elem."\n";
	}
	return $the_msg;
}
function error_text($num, $fieldname = "") {
	$fieldname = str_replace("_", " ", $fieldname);
	switch ($this->language) {
		case "fr":
		$msg[0]  = "SVP corriger les prochaines erreurs:";
		$msg[1]  = "Le champ <b>".$fieldname."</b> est vide.";
		$msg[10] = "Le date dans le champ <b>".$fieldname."</b> n\'est pas valide.";
		$msg[11] = "L'addresse d'email <b>".$fieldname."</b> n\'est pas valide.";
		$msg[12] = "Le valeur dans le champ <b>".$fieldname."</b> n\'est pas valide.";
		$msg[13] = "Le texte dans le champ <b>".$fieldname."</b> est trop long.";
		$msg[14] = "L'addresse de l\'internet dans le champ <b>".$fieldname."</b> n\'est pas valide.";
		$msg[15] = "Le champ <b>".$fieldname."</b> contient l\'HTML. Ce n\'est pas permit";
		break;
	default:
		$msg[0] = "Please correct the following error(s):";
		$msg[1] = "The field <b>".$fieldname."</b> is empty.";
		$msg[10] = "The date in field <b>".$fieldname."</b> is not valid.";
		$msg[11] = "The e-mail address in field <b>".$fieldname."</b> is not valid.";
		$msg[12] = "The value in field <b>".$fieldname."</b> is not valid.";
		$msg[13] = "The text in field <b>".$fieldname."</b> is too long.";
		$msg[14] = "The url in field <b>".$fieldname."</b> is not valid.";
		$msg[15] = "There is html code in field <b>".$fieldname."</b>, this is not allowed.";
	}
	return $msg[$num];
}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.