Form Tools
keeping form data on error validation - Printable Version

+- Form Tools (https://forums.formtools.org)
+-- Forum: Form Tools (https://forums.formtools.org/forumdisplay.php?fid=1)
+--- Forum: API (https://forums.formtools.org/forumdisplay.php?fid=17)
+--- Thread: keeping form data on error validation (/showthread.php?tid=327)



keeping form data on error validation - robotech - Oct 9th, 2009

Hi
I have a small test for that i am trying to get to keep the data thats entered and refill after a failed validation.
The form will validate just fine and reports the error but will not keep the Form data

Here is the sample of the code attached.any help with getting the form to re populate the fields on a error would be great

<?php
require_once("forms2/global/api/api.php");
$fields = ft_api_init_form_page(10);

$errors = array();
if (isset($_POST['submit']))
{
$rules = array();

$rules[] = "required,name,Please enter your name.";
$rules[] = "required,email,Please enter your email address.";
$rules[] = "valid_email,email,Please enter a valid email address.";
$rules[] = "required,comments,Please enter your comments.";
$errors = validate_fields($_POST, $rules);
if (empty($errors))
{



$params = array(
"submit_button" => "submit",
"next_page" => "thanks.php",
"form_data" => $_POST,
"file_data" => $_FILES,
"finalize" => true
);
ft_api_process_form($params);
}
}

?>


<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>

<body>

<?php
// if $errors is not empty, the form must have failed one or more validation
// tests. Loop through each and display them on the page for the user
if (!empty($errors))
{
echo "<div class='error'>Please fix the following errors:\n<ul>";
foreach ($errors as $error)
echo "<li>$error</li>\n";

echo "</ul></div>";
}
?>



<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="POST">



<td>

<p class="ft_font">Items Marked with a * are required fields
<hr size="1" style="width: 500px" class="style1" />
</p>




<table cellpadding="2" width="700" height="299" border="1" style="border-collapse: collapse" bordercolor="#808000">
<input type="hidden" name="NanProperties Contact Us" value="hidden" />
<tr>
<td class="required_field">*</td>
<td><b>Name</b></td>
<td class="answer"height="19">
<input type="text" name="name" maxlength="50" style="width:175px" <?php echo htmlspecialchars(@$fields['name']); ?> /></td>
</tr>
<tr>
<td class="required_field">*</td>
<td><b>Email</b></td>
<td class="answer"height="19"><input type="text" name="email" maxlength="20" style="width:150px" <?php echo htmlspecialchars(@$fields['email']); ?>/></td>
</tr>




<td class="required_field"></td>
<td><strong><font size="1">Please enter
</font>
</strong>
<font size="1">
<strong>Security Code:</strong>
</font>
</td>
<td class="answer"height="19"></td>


<tr>
<td></td>
<td><b>Comments</b></td>
<td class="answer"height="193">
<textarea name="comments" <?php echo htmlspecialchars(@$fields['comments']); ?> style="width: 319; height: 158"></textarea>/></td>
</tr>
</table>



<p><input type="submit" name="submit" value="Submit" style="float: center"></p>



</body>

</html>


Thanks Robotech


RE: keeping for data on error validation - msaz87 - Oct 9th, 2009

You're missing an else statement in your code...

Change this portion of your code:
PHP Code:
$params = array(
"submit_button" => "submit",
"next_page" => "thanks.php",
"form_data" => $_POST,
"file_data" => $_FILES,
"finalize" => true
);
ft_api_process_form($params);
}
}

?>

To:

PHP Code:
$params = array(
"submit_button" => "submit",
"next_page" => "thanks.php",
"form_data" => $_POST,
"file_data" => $_FILES,
"finalize" => true
);
ft_api_process_form($params);
} else {
$fields array_merge($fields$_POST);         
        
}


And see if that does the trick...


RE: keeping for data on error validation - robotech - Oct 10th, 2009

Hi
Thanks for the idea but its still the same I add the else stament as indicated, but the form will not keep its filled in data just returns with a blank form.
It will validate fine when all fields are completed correctly? and it posts to FT data base fine

Robotech


RE: keeping form data on error validation - msaz87 - Oct 10th, 2009

Ok I got it figured out...

The error was actually within the form itself... the calls to the values need to be in a value=" ". This is one you had:

PHP Code:
<input type="text" name="email" maxlength="20" style="width:150px" <?php echo htmlspecialchars(@$fields['email']); ?>/> 

And it needs to be:

PHP Code:
<input type="text" name="email" maxlength="20" style="width:150px" value="<?php echo htmlspecialchars(@$fields['email']); ?>"/> 

For the text area, it's a little different...

PHP Code:
<textarea name="comments" <?php echo htmlspecialchars(@$fields['comments']); ?> style="width: 319; height: 158"></textarea>/> 

Should be...

PHP Code:
<textarea name="comments" style="width: 319; height: 158"><?php echo htmlspecialchars(@$fields['comments']); ?></textarea>/> 

That worked for me when I duplicated your form. I left my previous response about adding the "else" command in the API code, so if you took it out and that doesn't work, just try sticking it back in there.

Hope that helps!


RE: keeping form data on error validation - robotech - Oct 10th, 2009

Thanks you this worked like a charm keeps all the field.

Thanks for the help

Robotech


RE: keeping form data on error validation - M8R-f08t5x - Mar 31st, 2012

>Thanks you this worked like a charm keeps all the field.

Yep, it does. Even if I go through the form and the "thank you page" and try to fill out new form, the old values are still there.

This doesn't empty the values on the thank you page:

ft_api_delete_unfinalized_submissions(7, false);
$fields = ft_api_init_form_page(7);
ft_api_clear_form_sessions(7);

What is wrong?


RE: keeping form data on error validation - msaz87 - Mar 31st, 2012

(Mar 31st, 2012, 1:18 PM)M8R-f08t5x Wrote: >Thanks you this worked like a charm keeps all the field.

Yep, it does. Even if I go through the form and the "thank you page" and try to fill out new form, the old values are still there.

This doesn't empty the values on the thank you page:

ft_api_delete_unfinalized_submissions(7, false);
$fields = ft_api_init_form_page(7);
ft_api_clear_form_sessions(7);

What is wrong?

Sounds like the sessions aren't clearing properly. I don't really use Form Tools anymore, so I can't tell you exactly what's wrong, but you could try globally unsetting sessions at the bottom of the page. This will clear all the sessions, so if you have any others active, this wouldn't be the way to go.

Try putting this at the bottom of the Thank You page:

PHP Code:
<?php session_unset(); ?>



RE: keeping form data on error validation - M8R-f08t5x - Jul 23rd, 2012

>This will clear all the sessions, so if you have any others active, this wouldn't be the way to go.

Yes it clears all the sessions. If there 2 users simultaneously, the other will go to error 305.

How to clear only the session which is being sent to database?


RE: keeping form data on error validation - msaz87 - Jul 23rd, 2012

(Jul 23rd, 2012, 1:36 PM)M8R-f08t5x Wrote: >This will clear all the sessions, so if you have any others active, this wouldn't be the way to go.

Yes it clears all the sessions. If there 2 users simultaneously, the other will go to error 305.

How to clear only the session which is being sent to database?

Try identifying the session you want to kill by viewing all the open ones using this:

PHP Code:
<?php print_r($_SESSION?>



RE: keeping form data on error validation - M8R-f08t5x - Jul 23rd, 2012

Ok, thanks. How do I put it together with the earlier line:

<?php session_unset(); ?>

My coding skills are limited Sad