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
[SOLVED] Has anyone gotten server-side validation to work?
#1
If you have a working sample, PLEASE send it out!

I've seen the latest tutorials working, but they use rsv (javascript) on the client side.

I've read a few post with the same problem than mine: an empty form is returned if validation fails (post here http://forums.formtools.org/showthread.p...ight=valid)

I'll test the whole page you send me in my server and I'll tart replacing small bites. This is driving me crazy and I have to track down the error.

Thanks.
Reply
#2
Hey Sergio,

Unless we can locate & fix the problem where process.php is failing to set the error info in sessions, server-side validation won't work on your server. See my previous post about contacting me with your login info.

- Ben
Reply
#3
Ben, I've sent you the login info to your email.

I'd save you the time, I know you must be very busy. Just send me an example of server-side validation php with the API and I'll change the FT path and form number to test it in my server!

Thanks.
Reply
#4
The only way I have got server side validation to work on my system without returning an empty form if validation fails is to use $_POST instead of $fields as given in the online examples.

e.g.

PHP Code:
<?=htmlspecialchars($_POST["fullname"])?>

instead of...

PHP Code:
<?=htmlspecialchars($fields["fullname"])?>
Reply
#5
Ben has been kind enough to try the error right in my form, so here's his message plus a few edits.

Replace your if(empty($errors)) statement by the following:
(replace your button's name attribute, next page finlename with yours)

PHP Code:
if (empty($errors)) {
      
$params = array(
        
"submit_button" => "contact_submit",
        
"next_page" => "thankyou.php",
        
"form_data" => $_POST,
        
"finalize" => true
      
);
    
      
ft_api_process_form($params);
        
$fields array_merge($fields$_SESSION["form_tools_form"]);
    } else {
      
$fields array_merge($fields$_POST);
  }
}
?>

There were two situations that weren't working:
(1) When the PHP validation failed (e.g. invalid email address), $fields wasn't being updated with the latest & greatest form field content. So these lines take care of that:

else
$fields = array_merge($fields, $_POST);

(2) when the user entered CAPTCHA incorrectly, again the $fields variable wasn't being updated with the latest content. So what I did was merge the content stored in $_SESSIONS (which IS up to date) with $fields. This ensures $fields has the latest content for displaying in the page.

$fields = array_merge($fields, $_SESSION["form_tools_form"]);

In terms of a long term fix, I'm going to have to think some more about this. Issue (1) is something that will have to be handled in the form code, so that won't change. But for (2), it seems like ft_api_process_form() should return an updated version of $fields when the user fails CAPTCHA. So expect an update to the API at some point that makes that step a little simpler (don't worry, though - it will be compatible with this code so you can upgrade in safety).
Reply
#6
I've also run into this problem but have been unable to solve it using the above solution.

I added the line
PHP Code:
else
      
$fields array_merge($fields$_POST); 

but the page still loses all the form data when server side validation fails.

Can anyone help?
Reply
#7
I have now managed to get server-side validation to work using Ben's solution. Integrated into my code (which only creates database entries when the form is submitted) Ben's solution looks like this...

PHP Code:
<?php

require_once("/httpdocs/formtools/global/api/api.php");

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

  
$rules[] = "required,fm_name,Please enter your name.";
  
$rules[] = "required,fm_email,Please enter your email address.";
  
$rules[] = "valid_email,fm_email,Please enter a valid email address.";
  
$rules[] = "required,fm_subject,Please enter a subject.";
  
$rules[] = "required,fm_message,Please enter a message.";

  
$errors validate_fields($_POST$rules);
 
  if(empty(
$errors))
  {
    
$fields ft_api_init_form_page(1);

    
$params = array(
      
"submit_button" => "form_submitted",
      
"next_page" => "thank-you.php",
      
"form_data" => $_POST,
      
"finalize" => true
    
);

    
ft_api_process_form($params);
    
$fields array_merge($fields$_SESSION["form_tools_form"]);
  }
  else
  {
    
$fields array_merge($fields$_POST);
  }
}
?>

And just above the form...

PHP Code:
<?php
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>\n"
}
?>

The only difference is that I have used the following PHP in the form...

PHP Code:
<?php echo htmlspecialchars(@$fields['fm_name']); ?>

...instead of as in the online examples...

PHP Code:
<?=htmlspecialchars(@$fields['fm_name'])?>
Reply
#8
(Aug 21st, 2009, 12:21 AM)spenny Wrote: I've also run into this problem but have been unable to solve it using the above solution.

I added the line
PHP Code:
else
      
$fields array_merge($fields$_POST); 

but the page still loses all the form data when server side validation fails.

Can anyone help?

I think it wasn't matter of just "adding" the line to your code. The code we had before had the IFs conditionals nested different.

Make sure you added the other line I posted and also the braces are balanced the same (resulting in a diferent nesting of conditionals)
Reply
#9
Has anyone run into trouble keeping the values in drop downs?

When I try to put in the documented code for mine:
PHP Code:
        <select name="Played_Before" class="registration">
            <
option <?php if ($fields["Played_Before"] == "Select One") echo "selected"?>>Select One</option>
            <option <?php if ($fields["Played_Before"] == "Yes") echo "selected"?>>Yes</option>
            <option <?php if ($fields["Played_Before"] == "No") echo "selected"?>>No</option>                    
        </select> 

It spits back the following on loading the page:
PHP Code:
        <select name="Played_Before" class="registration">
            <
option <br />
<
b>Notice</b>:  Undefined index:  Played_Before in <b>/home/fasports/public_html/az/phx/includes/league_registration_new.php</bon line <b>98</b><br />

>
Select One</option>
            <
option <br />
<
b>Notice</b>:  Undefined index:  Played_Before in <b>/home/fasports/public_html/az/phx/includes/league_registration_new.php</bon line <b>99</b><br />
>
Yes</option>
            <
option <br />
<
b>Notice</b>:  Undefined index:  Played_Before in <b>/home/fasports/public_html/az/phx/includes/league_registration_new.php</bon line <b>100</b><br />

>
No</option>                    
        </
select

If you run a test of the form, it clears the values back to normal and if you try it a second time, it works... but obviously I need it to work the first time around.

The page being worked on is:
http://fasports.com/az/phx/mondays_new.php -- but it might be changing as I'm still playing around with it

Any help is appreciated... thanks!
Reply
#10
Hi msaz87,

Ah - this is due to the PHP error notification level. I have it cranked up really high so that I can hear about all problems like this. You can fix it in two ways:

1. In your config.php file, add the following line:
PHP Code:
$g_default_error_reporting 1

That will tell it to only report genuine errors, not notices (minor, potential problems) like the ones you described.

2. In your form, add a "@" symbol before referencing the $fields variables. This suppresses minor warnings/notifications, like undefined indexes. For example:
Code:
<select name="Played_Before" class="registration">
            <option <?php if (@$fields["Played_Before"] == "Select One") echo "selected"; ?>>Select One</option>
            <option <?php if (@$fields["Played_Before"] == "Yes") echo "selected"; ?>>Yes</option>
            <option <?php if (@$fields["Played_Before"] == "No") echo "selected"; ?>>No</option>                    
        </select>

Hope this helps!

- Ben
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)