Jump to content

Reloading a form page and keeping the filled-in fields?


adamevans

Recommended Posts

I feel stupid, because I know this is a simple thing, but I can't seem to get it.

 

I'm making a registration system with lots of fields to fill in.  When you submit it, if there's an error (like a field not filled in, or whatever), I don't want it to just die.  I want it to reload with the forms still filled in but with the appropriate error message displayed. 

 

I've seen this on plenty of sites, but I can't seem to figure this out.  I can get a blank form to reload, but that's it.

Link to comment
Share on other sites

The easiest way is to set the value of your form fields to the post value...

 

<input type="text" size="25" name="last_name" value="<?php echo $_POST['last_name']; ?>" />

 

However, if you have error reporting set to E_ALL a notice will be generated the first time the user visits the page because $_POST isn't set before the page is submitted.  Just reduce the error reporting level to make it go away.

Link to comment
Share on other sites

I think there is a encoded string that you put in your html code to make this. so that when the user goes back to the form page because of an error displayed by script that values are still in the form. It was mentioned in one of the ASP.NET books that i have but i cant seem to find it for ya.

 

Anyway, the way i use is templates.

 

$Temp->clearParams();
$Temp->setFilename("form_name.tpl");
$Temp->addParam("Name",$Web->POST("name"));
$Temp->addParam("School",$Web->POST("school"));
.
.
.
$Content = $Temp->Compile();

 

here is the template class written by me

 

<?
/** 
* A Templates class
* @author: Rakan Alhneiti
* @version: 1.0
*/
class Template
{
/** @var Filename of the template file.*/
var $Template_Filename;

/** @var Array of tags and their values.*/
var $Replacements = array();

/** @var Language variable */
var $Language;

/** @var Application specific language text array */
var $Application_Language = array();

/**
	* Sets & gets the application specific language
*/
	function setAppLang($Arr)
	{
		$this->Application_Language = $Arr;
	}
	function getAppLang()
	{
		return $this->Application_Language;
	}


/**
	* Sets the language of the page
*/
	function setLanguage($lang)
	{
		$this->Language = $lang;
	}

/**
	* Sets the filename the the template file
	*/
	function setFilename($file)
	{
		if($this->Language != "")
			$this->Template_Filename = "templates/".$this->Language."/".$file;
		else 
			$this->Template_Filename = "templates/".$file;
	}

	/**
	* @return void
	* @param Key of tag to be repalced
	* @param Value of tag
	*/
	function addParam($Key,$Value)
	{
		$this->Replacements[$Key] = $Value;	
	}

	/**
	* @return void
	*/
	function clearParams()
	{
		unset($this->Replacements);
		$this->Replacements = array();
	}

	/**
	* @return string Gets the contents of the template file
	* @param string Filename of template file.
	*/
	function getContent()
	{
		$File = fopen($this->Template_Filename,'r');
		$Content = fread($File,filesize($this->Template_Filename));
		fclose($File);
		return $Content;
	}

	/**
	* @return Replaces all tags with their values
	*/
	function Compile()
	{
		if($this->Language!="");
			$this->addParam("Lang",$this->Language);

		$Content = $this->getContent();

		if(sizeof($this->Application_Language)>0)
		{
			while(list($Col,$Val)=each($this->Application_Language))
			{
				if(strpos($Content,$Col,0)!=false)
					$Content = str_replace("{".$Col."}",$Val,$Content);		
			}
		}
		reset($this->Application_Language);


		if(sizeof($this->Replacements)>0)
		{
			while(list($Index,$Val) = each($this->Replacements) ) 
			{
				$Content = str_replace("{".$Index."}",$Val,$Content);
			}
		}	
		return $Content;
	}
}


?>

 

Web class

<?
/** 
* A Web class
* @author: Rakan Alhneiti
* @version: 1.0
*/

class Web
{

	/**
	* @return mixed $_GET[$id] value
	* @param string The index or key for the GET value that should be returned.
	*/
	function GET($id="")
	{
		if (@isset($_GET[$id]) && trim($_GET[$id])!="")
		{
 			return trim($this->clearSqlInjections($_GET[$id]));
		}
		else
		{
			return "";
		}
	}

	/**
	* @return mixed $_GET[$id] value
	* @param string The index or key for the POST value that should be returned.
	*/
	function POST($id="")
	{
		if(@isset($_POST[$id]) && ! @empty($_POST[$id]))
			return trim($this->clearSqlInjections($_POST[$id]));
		else 
			return "";
	}

	function scanEmail($Item)
	{
		if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Item)) 
		{ 
			return false; 
		}
		else
		{ 
         		return true;
		} 
	} 

	function clearSqlInjections($Item="")
	{
		//character 39 = ' (Single quote)
		if(strpos($Item,chr(39))!=false)
		{
			$Item = str_replace(chr(39),"",$Item);
		}
		//character 60 = <
		if(strpos($Item,chr(60))!=false)
		{
			$Item = str_replace(chr(60),"&lt",$Item);
		}
		//character 61 = "=" (equal sign)
		if(strpos($Item,chr(61))!=false)
		{
			$Item = str_replace(chr(61),"",$Item);
		}
		//character 62 = >
		if(strpos($Item,chr(62))!=false)
		{
			$Item = str_replace(chr(62),"&gt",$Item);
		}
		return $Item;
	}
}
?>

Link to comment
Share on other sites

That is a feature of ASP.NET...it's called "View State"...essentially it's an array that is serialized, then base64 encoded, then sent as a hidden form field to the browser so that the next time the page is posted to the server it can see what the previous values were for any form fields.

 

Such a thing does not exist by default in php, however I'm sure there are many user classes that do the same thing.  Search www.phpclasses.org....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.