FORUMS


The Form Tools forums are no longer active, but the old posts have been archived here. Please see the Help page on how to get help / report issues.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
update fields after failed validation
#1
Hi,

I followed the "Adding our validation" tutorial and it works well. My problem is that when the validation failed, some fields are not updated (the menu type and text area).
Is there a way to have all fields updated?

Thanks
Reply
#2
Hi tfm,

Welcome to the forums! Smile

This is with PHP validation, I take it? Could you post your PHP code so I could take a look at it?

- Ben
Reply
#3
Hi Ben and thanks in advance to take a look at my code.

PHP Code:
<?php require_once("/home/a5060614/public_html/formtools_2.1.0._Beta/global/api/api.php");
$fields ft_api_init_form_page(6);

// validation time!
$errors = array();
if (isset(
$_POST['Submit']))
{
  
$rules = array();
  
$rules[] = "required,nom,Veuillez entrer votre nom";
  
$rules[] = "required,email,Veuillez entrer votre adresse email";
  
$rules[] = "valid_email,email,Veuillez entrer une adresse email valide";
  
$rules[] = "required,code_postal,Veuillez indiquer votre code postal";
  
$rules[] = "digits_only,code_postal,Votre code postal ne doit comporter que des chiffres";
  
$rules[] = "required,pays,Veuillez indiquer votre pays";
  
$rules[] = "required,type,Veuillez indiquer le type de travaux que vous désirez";
  
$rules[] = "required,delai,Veuillez indiquer le délai que vous désirez";
  
$errors validate_fields($_POST$rules);
 
  
// no errors - great! Now we process the page. The ft_api_process_form does
  // the job of both updating the database and redirecting to the next page
  
if (empty($errors))
  {
    
$params = array(
      
"submit_button" => "Submit",
      
"next_page" => "Merci.php",
      
"form_data" => $_POST,
      
"file_data" => $_FILES,
      
"finalize" => true
    
);
    
ft_api_process_form($params);
  }
  
// it failed validation. Update $fields with the latest contents of the form data
  
else 
  {
    
$fields array_merge($_SESSION["form_tools_form"], $_POST);
  }
}
?>
Reply
#4
Hi tfm,

Ah! The problem actually turned out to be just with the markup.

Dropdowns all need to have value="" attributes for the options. In the absence of the value attribute, some browsers send along whatever the content of the option tag is, but it's not standardized. So change this:

Code:
<select name="delai" id="delai">
  <option />
  <option>1 semaine</option>
  <option>1 mois</option>
  <option>Plus</option>
</select>

To this:

Code:
<select name="delai" id="delai">
  <option value=""></option>
  <option value="1 semaine">1 semaine</option>
  <option value="1 mois">1 mois</option>
  <option value="Plus">Plus</option>
</select>

Then, add in the PHP code to have it re-filled:

Code:
<select name="delai" id="delai">
  <option value="" <?php if (@$fields["delai"] == "") echo "selected"; ?>></option>
  <option value="1 semaine" <?php if (@$fields["delai"] == "1 semaine") echo "selected"; ?>>1 semaine</option>
  <option value="1 mois" <?php if (@$fields["delai"] == "1 mois") echo "selected"; ?>>1 mois</option>
  <option value="Plus" <?php if (@$fields["delai"] == "Plus") echo "selected"; ?>>Plus</option>
</select>

For the textarea, don't bother with the value="" attribute. To re-fill a textarea, you need to output th content right within the tag itself:

Code:
<textarea name="description" cols="50" rows="5" id="description"><?php echo @$fields["description"]; ?></textarea>

Hope this helps -

Ben

Reply
#5
Thanks a lot but I still have one problem.
It works well for the text area but with the dropdowns, I have a syntax error when I add the php code. At the same time, it creates an error in the textarea section... Do I have to add something before the php code?
Reply
#6
Hi tfm,

Sorry for the long wait in responding!

Could you post the HTML and PHP for your dropdown here? It's probably just a small little syntax error in the code.

- Ben
Reply
#7
Hi Ben,

Here is all the php code I used :
PHP Code:
<?php require_once("/home/a5060614/public_html/formtools_2.1.0._Beta/global/api/api.php");
$fields ft_api_init_form_page(2);

// validation time!
$errors = array();
if (isset(
$_POST['Submit']))
{
  
$rules = array();
  
$rules[] = "required,nom,Veuillez entrer votre nom / Please enter your name";
  
$rules[] = "required,email,Veuillez entrer votre adresse email / Please enter your email address";
  
$rules[] = "valid_email,email,Veuillez entrer une adresse email valide / Please enter a valid email address";
  
$rules[] = "required,code_postal,Veuillez indiquer votre code postal / Please enter your zip code";
  
$rules[] = "digits_only,code_postal,Votre code postal ne doit comporter que des chiffres / Your zip code should only contain digits";
  
$rules[] = "required,pays,Veuillez indiquer votre pays / Please enter your country of residence";
  
$rules[] = "required,type,Veuillez indiquer le type de travaux que vous désirez / Please enter the kind of work you want";
  
$rules[] = "required,delai,Veuillez indiquer le délai que vous désirez / Please enter the deadline for your work";
  
$errors validate_fields($_POST$rules);
 
  
// no errors - great! Now we process the page. The ft_api_process_form does
  // the job of both updating the database and redirecting to the next page
  
if (empty($errors))
  {
    
$params = array(
      
"submit_button" => "Submit",
      
"next_page" => "Merci.php",
      
"form_data" => $_POST,
      
"file_data" => $_FILES,
      
"finalize" => true
    
);
    
ft_api_process_form($params);
  }
  
// it failed validation. Update $fields with the latest contents of the form data
  
else 
  {
    
$fields array_merge($_SESSION["form_tools_form"], $_POST);
<
select name="delai" id="delai">
  <
option value="" <?php if (@$fields["delai"] == "") echo "selected"?>></option>
  <option value="1 semaine" <?php if (@$fields["delai"] == "1 semaine") echo "selected"?>>1 semaine</option>
  <option value="1 mois" <?php if (@$fields["delai"] == "1 mois") echo "selected"?>>1 mois</option>
  <option value="Plus" <?php if (@$fields["delai"] == "Plus") echo "selected"?>>Plus</option>
</select>
  }
  
}

?>
<!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" lang="fr-fr">
<head> 
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
    <title>Demande de devis</title>    
      <link media="all" type="text/css" rel="stylesheet" href="CommTat.css" />
</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'>Veuillez résoudre les erreurs suivantes :\n<ul>";
  foreach (
$errors as $error)
    echo 
"<li>$error</li>\n";
 
  echo 
"</ul></div>"
}

?>

The "<select name="delai" id="delai">" line indicates a syntax error. I tried to put it somewhere else with the same result...

Here is the html for the dropdown :
Code:
<select name="delai" id="delai">
        <option value=""></option>
    <option value="1 semaine">1 semaine</option>
    <option value="1 mois">1 mois</option>
    <option value="Plus">Plus</option>
</select>

Thanks once again
Reply
#8
Hi tfm,

Ah! Yeah, it's this bit. Change these lines:

Code:
// it failed validation. Update $fields with the latest contents of the form data
else
{
$fields = array_merge($_SESSION["form_tools_form"], $_POST);
<select name="delai" id="delai">
<option value="" <?php if (@$fields["delai"] == "") echo "selected"; ?>></option>
<option value="1 semaine" <?php if (@$fields["delai"] == "1 semaine") echo "selected"; ?>>1 semaine</option>
<option value="1 mois" <?php if (@$fields["delai"] == "1 mois") echo "selected"; ?>>1 mois</option>
<option value="Plus" <?php if (@$fields["delai"] == "Plus") echo "selected"; ?>>Plus</option>
</select>
}

}

To this:

Code:
// it failed validation. Update $fields with the latest contents of the form data
else
{
$fields = array_merge($_SESSION["form_tools_form"], $_POST);
?>
<select name="delai" id="delai">
<option value="" <?php if (@$fields["delai"] == "") echo "selected"; ?>></option>
<option value="1 semaine" <?php if (@$fields["delai"] == "1 semaine") echo "selected"; ?>>1 semaine</option>
<option value="1 mois" <?php if (@$fields["delai"] == "1 mois") echo "selected"; ?>>1 mois</option>
<option value="Plus" <?php if (@$fields["delai"] == "Plus") echo "selected"; ?>>Plus</option>
</select>
<?php
}

}

- Ben
Reply
#9
Hi Ben,

Thanks, the error disappeared but my fields were still blank after a submitted error.
I looked again through various websites and I have found a solution I missed before. Here it is for those who will be interested:

I replaced :
Code:
<select name="delai" id="delai">
  <option value=""></option>
  <option value="1 semaine">1 semaine</option>
  <option value="1 mois">1 mois</option>
  <option value="Plus">Plus</option>
</select>
by
Code:
<select name="delai[]" id="delai">
  <option value="" <?php if (@in_array("", $fields["delai"])) echo "selected"; ?>></option>
  <option value="1 semaine" <?php if (@in_array("1 semaine", $fields["delai"])) echo "selected"; ?>>1 semaine</option>
  <option value="1 mois" [quote]<?php if (@in_array("1 mois", $fields["delai"])) echo "selected"; ?>>1 mois</option>
  <option value="Plus" <?php if (@in_array("Plus", $fields["delai"])) echo "selected"; ?>>Plus</option>
</select>

And I didn't need the additional code in the php section.

Thanks for your time
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)