Jump to content

[SOLVED] data only gets passed on second page refresh?


blueman378

Recommended Posts

Hi guys.

 

well im having a problem with my code,

 

basically when an error gets set it stores it in a class variable

this is at the same time meant to store it in a session.

 

it should then on page refresh set the class variable to that of the session.

 

but the problem im having is if i submit the form(with errors) the session gets update however the class variable is still empty, but then next time i reload the page the session data then gets put into the class variable.

 

i cant see why it takes a second page reload to do this?

 

can anyone see.

 

<?php
session_start();
include("form.php");
$form->addvaltype("alphanumtest", "alphanum");
$form->addvaltype("lol", "numeric");
$form->addvaltype("alphanumtest2", "alphanum");
if($_POST)
{
$form->validate();
}

echo "session data: ";
print_r($_SESSION['error_array']);
echo "<br />\$form data: ";
print_r($form->errors);
?>
<form action="" method="post"><br />
<input name="alphanumtest" type="text"><br />
<?php echo $form->error("alphanumtest"); ?><br />
<input name="lol" type="text"><br />
<?php echo $form->error("lol"); ?><br />
<input name="alphanumtest2" type="text"><br />
<?php echo $form->error("alphanumtest2"); ?><br />
<input type="submit">
</form>

 

<?php
if(session_id() == "")
{
session_start();
}

class form
{
   var $values = array();
   var $errors = array();
   var $valtype = array();
   var $num_errors;

   /* Class constructor */
   function form(){
      if(isset($_SESSION['error_array'])){
         $this->values = $_SESSION['value_array'];
         $this->errors = $_SESSION['error_array'];
         $this->num_errors = count($this->errors);
         unset($_SESSION['value_array']);
         unset($_SESSION['error_array']);
      }
      else{
         $this->num_errors = 0;
      }
   }
   function setValue($field, $value){
      $this->values[$field] = $value;
   }
   
   function setError($field, $errmsg){
	$this->errors[$field] = $errmsg;
	$this->num_errors = count($this->errors);
	$_SESSION['value_array'] = $this->values;
        $_SESSION['error_array'] = $this->errors;
   }

   function value($field){
      if(array_key_exists($field,$this->values)){
         return htmlspecialchars(stripslashes($this->values[$field]));
      }else{
         return "";
      }
   }

   function error($field){
      if(array_key_exists($field,$this->errors)){
         return "<font size=\"2\" color=\"#ff0000\">".$this->errors[$field]."</font>";
      }else{
         return "";
      }
   }

   function getErrorArray(){
      return $this->errors;
   }
   
   function addvaltype($field, $valtype)
{
	$this->valtype[$field] = $valtype;
}

function validate()
{
	$i = 0;
	foreach ($this->valtype as $field => $val) 
	{
		global $$val;
		$$val->validate($_POST[$field], $field);
	}
}
}

$form = new form;

if ($handle = opendir('validations')) {
    while (false !== ($file = readdir($handle))) {
    	if($file != "." && $file != "..")
    	{
        	include("validations/$file");
        }
    }
    closedir($handle);
}

?>

 

<?php

// VALIDATION TYPE:	alphanum
// VALIDATION DESC:	only allow letters and numbers

class alphanum extends form
{
function alphanum()
{
}

function validate($input, $field)
{
	if(!isset($input) || $input == "" || $input == NULL)
	{
		$this->setError($field, "$field cannot be left empty!");
		return 1;
	}
	elseif(!ctype_alnum($input))
	{
		$this->setError($field, "$field can only contain letters [a-z] and numbers [0-9]!");
		return 1;	
	}
	else
	{
		return 0;
	}
}

}

$alphanum = new alphanum;
?>

 

 

edit: heh i didnt think that [ code=pagetitle ][/code] would work

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.