Jump to content

Form validation - retaining field values?


bladechob

Recommended Posts

Hi, I've got a number of forms that I need to 'keep' the filled fields if a validation error has been picked up. Who wants to go through the process of filling out a form again if they've forgotten to fill in a field? Here's an example of the file that writes it to the database, fires off an email and has a result message (it's called in by addAgent_form.php)

<?php

include_once('cfg.php');
include_once($CFG->class_dir.'/dataObjects/iwantajob/Agent.php');
include_once('User.php');
include_once($CFG->class_dir."/webComponents/SecureForm.php");
include_once($CFG->class_dir."/webComponents/formelements.php");
include_once($CFG->class_dir."/webComponents/formlib.php");

error_reporting($debug);
$success=false;
$result_msg='';

// session check - only legit users can do this

// form data checks

$method=&$_POST;

clean_form_vars($method);


// processForm specifics

function processForm(){
global $CFG, $conn, $method, $now, $success, $result_msg;

$a=new Agent();

$a->setAgentName($method['agentName']);
$a->setAddress1($method['address1']);
$a->setAddress2($method['address2']);
$a->setCity($method['city']);
$a->setPostcode($method['postcode']);
$a->setCountry($method['country']);
$a->setTelephone($method['telephone']);
$a->setFax($method['fax']);
$a->setWebsite($method['website']);
$a->setEmail($method['email']);



if(!$newAgent=$a->insert()){
	$success=false;
	$result_msg="Sorry. There has been a problem writing your details to the database";
}else{
	mysql_select_db('users');
	$query="select * from users where username='".$method['username']."'";
	if(!$result=mysql_query($query,$conn)){
		echo mysql_error();
	}else{
		if(mysql_num_rows($result)!=0){
			$success=false;
			$result_msg="Sorry. That user name is already in use. <a href=\"javascript:history.go(-1)\">Click here</a> to return to the form and try again";
		}else{
			$us=new User();
			$us->username=$method['username'];
			$us->password=md5($method['password']);
			$us->userType='Agent';
			$us->class='Agent';
			$us->classPath='dataObjects/iwantajob';
			$us->active='Y';
			$us->uniqueID=$newAgent;
			$us->dateRegistered=$now;

			$pass=$method['password']; //for email message

			if(!$newUser=$us->insert()){
				$success=false;
				$result_msg="Sorry. There has been a problem writing your user account to the database";
			}else{
				$success=true;
				$result_msg='Thank you. Your details have successfully been written to the database. <b><a href="index.php?content=login">Click here to log in and start adding jobs!</a></b>';
				mail($a->getEmail(),'Your iwantajob.uk.com user details',"Thank you for registering with iwantajob.uk.com\n\nYour account details are:\n\nUsername: $us->username\nPassword: $pass\n\nWe look forward to helping you in your search for suitable candidates for your vacancies.\n\nRegards, the iwantajob.uk.com team",'From:registration@iwantajob.uk.com');
			}
		} // end dead query
	} // end user already exists
} //end couldn't do agent 
}

if(validate(&$method,PROTECTED_KEY)){
	$isValid=validateForm($method);
}else{
	echo "invalid data!";
}

echo '<p class="errorList">'.$result_msg.'</p>';


?>
<?php
// get posted data into local variables
$EmailFrom = "agentregistration@iwantoajob.uk.com";
$EmailTo = "s.mackenzie@cim.uk.com";
$Subject = "Agent Registration";
$agentName = Trim(stripslashes($_POST['agentName'])); 
$address1 = Trim(stripslashes($_POST['address1'])); 
$address2 = Trim(stripslashes($_POST['address2'])); 
$city = Trim(stripslashes($_POST['city'])); 
$telephone = Trim(stripslashes($_POST['telephone'])); 
$email = Trim(stripslashes($_POST['email'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "agentName: ";
$Body .= $agentName;
$Body .= "\n";
$Body .= "address1: ";
$Body .= $address1;
$Body .= "\n";
$Body .= "address2: ";
$Body .= $address2;
$Body .= "\n";
$Body .= "city: ";
$Body .= $city;
$Body .= "\n";
$Body .= "telephone: ";
$Body .= $telephone;
$Body .= "\n";
$Body .= "email: ";
$Body .= $email;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");



?>

Link to comment
Share on other sites

Hi,

These are called "sticky forms".

You need to set the forms values to whatever has just been posted:

e.g.:

<input type="text" name="email" value="<?php $_POST['email'];?>" />
<input type="text" name="tel" value="<?php $_POST['tel'];?>" />

 

etc.

 

if you post to a different form the you need to set the values into a session then redirect back to the form and set the form values to the session values, very similar to above:

<input type="text" name="email" value="<?php $_SESSION['email'];?>" />
<input type="text" name="tel" value="<?php $_SESSION['tel'];?>" />

Link to comment
Share on other sites

Thanks for that Sulman, I'm just wondering if that is going to effect the field structure on the actual form page (php) as detailed below. I'm guessing the validation happens here (addAgent_form.php)...

<?php 

global $CFG, $conn;

// session check here - redirect to unauthorised page if userDetailsObject is not of type 'Agent'

$isValid=false;
$isSubmitted=false;

error_reporting($debug);

if(!empty($_POST['submit'])){

include_once('addAgent.php');
$isSubmitted=true;
if(!$isValid){
	displayErrors();	
}


}else{

include_once($CFG->include_dir.'/dblib.php');
include_once($CFG->class_dir.'/webComponents/SecureForm.php');
include_once($CFG->class_dir.'/webComponents/formelements.php');
include_once($CFG->class_dir."/webComponents/DropDownList.php");

$frm=new SecureForm('frm','POST',$_SERVER['REQUEST_URI'],'frm');


$un=new TextInputField('username','','',30,30);
$pw=new PasswordField('password','','',30,30);
$an=new TextInputField('agentName','','',30,30);
$sn=new TextInputField('surname','','',30,30);
$ad1=new TextInputField('address1','','',50,30);
$ad2=new TextInputField('address2','','',50,30);
$cty=new TextInputField('city','','',30,30);
$pc=new TextInputField('postcode','','',30,30);
$cny=new TextInputField('country','','',30,30);
$eml=new TextInputField('email','','',50,50);
$tel=new TextInputField('telephone','','',30,30);
$wbs=new TextInputField('website','','',30,30);
$fax=new TextInputField('fax','','',30,30);
$leg=new TextArea('legal','','',40,10,true);

$frm->requireField('username');
$frm->requireField('password');
$frm->requireField('agentName');
$frm->requireField('address1');
//$frm->requireField('address2');
$frm->requireField('city');
$frm->requireField('postcode');
$frm->requireField('country');
$frm->requireField('email');

?>
<script language="javascript" src="../js/calendar.js"></script>
<script language="javascript" src="../js/cal.js"></script>
<script language="javascript" src="../js/lang/calendar-en.js"></script>

<table style="margin-left:6px;">
<tr><td colspan="3"><h4>Employer & Agent registration</h4></td></tr>
<tr>
<td width="160"><h5>Please enter all details in the form.</h5></td>
	<td width="460">			
		<?php $frm->printHeader(); ?>
		<table width="460" cellspacing="0" cellpadding="0">
			<tr>
				<td class="formFieldGroup" colspan="3"><p>User account details: </p></td>
			</tr>
			<tr>
				<td class="formLegend" width="200"><span>User name: (6 - 15 characters) </span></td>
				<td width="10"> </td>
				<td class="formField"><?php $un->displayField(); ?></td>
			</tr>
			<tr>
				<td class="formLegend" width="200"><span>Password:</span></td>
				<td width="10"> </td>
				<td class="formField"><?php $pw->displayField(); ?></td>
			</tr>
			<tr>
				<td class="formFieldGroup" colspan="3"><p>Employer/Agent details:</p></td>
			</tr>
			<tr>
				<td class="formLegend"><span>Employer/Agent name:</span></td>
				<td> </td>
				<td class="formField"><?php $an->displayField(); ?></td>
			</tr>
			<tr>
				<td class="formLegend"><span>Address, line 1</span></td>
				<td> </td>
				<td class="formField"><?php $ad1->displayField(); ?></td>
			</tr>
			<tr>
				<td class="formLegend"><span>Address, line 2</span></td>
				<td> </td>
				<td class="formField"><?php $ad2->displayField(); ?></td>
			</tr>
							<tr>
				<td class="formLegend"><span>Town/City:</span></td>
				<td> </td>
				<td class="formField"><?php $cty->displayField(); ?></td>
			</tr>
							<tr>
				<td class="formLegend"><span>Postcode:</span></td>
				<td> </td>
				<td class="formField"><?php $pc->displayField(); ?></td>
			</tr>
							<tr>
				<td class="formLegend"><span>Country:</span></td>
				<td> </td>
				<td><?php $cny->displayField(); ?></td>
			</tr>
							<tr>
				<td class="formLegend"><span>Email address:</span></td>
				<td> </td>
				<td><?php $eml->displayField(); ?></td>
			</tr>
			<tr>
				<td class="formLegend"><span>Contact telephone:</span></td>
				<td> </td>
				<td class="formField"><?php $tel->displayField();  ?></td>
			</tr>
			<tr>
				<td class="formLegend"><span>Website:</span></td>
				<td> </td>
				<td class="formField"><?php $wbs->displayField();  ?></td>
			</tr>
			<tr>
				<td class="formLegend"><span>Fax:</span></td>
				<td> </td>
				<td class="formField"><?php $fax->displayField();  ?></td>
			</tr>
							<tr>
				<td class="formLegend"></td>
				<td> </td>
				<td class="formField"><input type="submit" value="submit" name="submit"></td>
			</tr>
		</table>
		<?php $frm->printFooter(); ?>
		<p> </p>
	</td>
	<td width="160"><!-- include rightBar --> </td>

</tr>
</table>
<?php } ?>[code]

[/code]

Link to comment
Share on other sites

Ah,

 

It appears your classes are creating the form input fields...This means that you will have to modify the classes to accept a value (if it doesn't already). Something like:

<?php
$fax->displayField($_POST['fax']);
?>

 

If you wrote the class you should be able to modify it ok. Otherwise you'll need to read up on the class and see what does what.

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.