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:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Re-Edit Form?
#1
I have a very complicated form (multi-page form using the API) which some users will not fill out all in one go. Instead they will need to complete it over a few days. I’d like to know if it’s possible for someone to submit the form on say a Monday and then the following day log in with a user name and password and continue editing it.

I’ve had a look at the Submission Accounts module but don’t think this is suitable because I can’t use it with the original form I have created. Is this right?

What I was hoping was there there’s a way that I could use one of the API functions to re-load all the $fields values for a submitted form so that the user could then re-edit the form?

Thanks in advance.
Reply
#2
Hi Spenny,

Sure! Actually I do this very thing for a few clients. I wrote the Submission Accounts module because people generally aren't that technical and it allows for this functionality without delving into any PHP - but it's NOT a perfect solution, I'll be the first to admit. And yes, you're right - the module doesn't use the original form.

If you're interested, post me back and when I get home tonight I'll locate some code for you. If memory serves it's not TOO tricky to get up and running; but it will require you a separate login form for your form. I'll explain that later...

- Ben
Reply
#3
Hi Ben,

That's sounds perfect! I'd be most grateful for the code that allows this. While I am not an expert PHP coder, I'm cetainly willing to have a go at configuring some code and setting up the login form.

Kind regards
Spenny
Reply
#4
Ah yes! Thanks for the reminder. Let me try and find an example now...

- Ben
Reply
#5
Alright! Here we go. I've tried to abstract away most of the details so you can just drop it into your own form with only a few minor changes. Not sure if it'll work precisely as planned, but let's give it a go.

What you need to do is add a new login form, say "login.php". This should contain the following - obviously you can separate the PHP and HTML if - I just stuck it a single file for this example for convenience.

The idea is that all users going to your form will be faced with a login form off the bat. New users who haven't registered yet can use the default password (usually I just display it on the page). Existing users can log in with their own password - which they would have changed on page 1 of your own form.

Well, code first. Explanation below:

PHP Code:
<?php

$folder 
dirname(__FILE__);
require_once(
"$folder/../admin/global/api/api.php");
ft_api_start_sessions();

$form_id 53;
$username_field "attendee_email"// the database column that's acting as your "username" field.
$password_field "password"// the database column acting as your "password" field
$default_password "pass123"// our default password. This allows anyone to register using this password, which they can change during the reg process


$email "";
if (isset(
$_POST["email"]) && !empty($_POST["email"]))
{
    
$email    ft_sanitize($_POST["email"]);
    
$password ft_sanitize($_POST["password"]);

    
// try to locate the user
    
$query mysql_query("
      SELECT *
      FROM   
{$g_table_prefix}form_{$form_id}
      WHERE  
$username_field = '$email' AND
             
$password_field = '$password'
        "
);
  
$result mysql_fetch_assoc($query);

  if (empty(
$result))
  {
      if (
$password != $default_password)
      {
        
$g_success false;
        
$g_message "Sorry, we couldn't locate that email/password combination. Please try again.";
      }
    else
    {
          
// this is a new registrant" create a record for them
          
if (empty($result))
          {
              
$default_values = array(
                
"attendee_status" => "Incomplete",
                
"$username_field=> $email,
                
"$password_field=> $default_password
              
);
              
$result["submission_id"] = ft_api_create_blank_submission($form_idtrue$default_values);
          }

          
$_SESSION["form_tools_form"] = $result;
          
$_SESSION["form_tools_form"][$username_field] = $email;
          
$_SESSION["form_tools_form"]["attendee_status"] = "Incomplete"// sets the form field "attendee_status" as 
          
$_SESSION["form_tools_form"]["form_tools_form_id"]       = $form_id;
          
$_SESSION["form_tools_form"][$password_field]       = $default_password;
          
$_SESSION["form_tools_form"]["form_tools_submission_id"] = $result["submission_id"];
          
header("location: step1.php");
          exit;
    }
  }
  else
  {
      
$_SESSION["form_tools_form"] = $result;
      
$_SESSION["form_tools_form"][$username_field] = $email;
      
$_SESSION["form_tools_form"]["form_tools_form_id"]       = $form_id;
      
$_SESSION["form_tools_form"]["form_tools_submission_id"] = $result["submission_id"];
      
header("location: step1.php");
      exit;
  }
}
?>
<html>
<head>
</head>
<body>

<h1>Log In</h1>

<?php
if ($g_message) echo $g_message;
?>

  <form method="post" action="<?=$_SERVER["PHP_SELF"]?>" name="login">

    <div class="" style="padding-top: 3px; width:400px">
        <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
              <td width="120" class="blue">Email:</td>
              <td><input type="text" size="25" name="email" id="email" value="<?=$email?>" tabindex="1" /></td>
                <td width="80" align="center" rowspan="2">
                  <input type="submit" value="LOG IN" tabindex="3" />
                </td>
            </tr>
        <tr>
              <td class="blue">Password</td>
              <td><input type="password" size="25" name="password" value="" tabindex="2" autocomplete="no" /></td>
            </tr>
        </table>
      </div>

  </form>
</body>
</html> 

1. Update this line to point to your own api.php location:
require_once("$folder/../admin/global/api/api.php");

2. Set the $form_id to your own form ID.

3. Change these three fields to store the database column names of the fields that are going to act as the user's login username and password. In order to uniquely identify the user for when they log back in, these two are required. I always use "email" as a username field - simplest that way.

$username_field = "attendee_email";
$password_field = "password";

Also: I'm pretty sure the code requires your form fields to have the same database column names. I.e. your email field has a name attribute of "attendee_email" AND your database column for that field has the same value. I've always done this - had a 1-1 relation between form field names and database column names. It just makes working with the database that much simpler. You don't HAVE to do this, but you may have to review the code to ensure it's all okay.

4. The default password. The form I was taking this from required a default password for all new users to log in with. Once they were logged in, they could change it. If you don't need this feature, just change it to some long, indecipherable string.

$default_password = "pass123";

5. Change these lines to specify the first page in your form (there are two lines like this):
header("location: step1.php")

6. On the first page of your form you'll need to add the option to change the default password. All this requires is adding a field to your Form Tools form with the name attribute the same as specified above. And secondly, you'll need to add it to Form Tools so the information is actually stored in the database.


And I think that's it... Hope this is somewhat helpful & not too unclear.
Reply
#6
(Aug 1st, 2009, 9:28 AM)Ben Wrote: Also: I'm pretty sure the code requires your form fields to have the same database column names. I.e. your email field has a name attribute of "attendee_email" AND your database column for that field has the same value. I've always done this - had a 1-1 relation between form field names and database column names. It just makes working with the database that much simpler. You don't HAVE to do this, but you may have to review the code to ensure it's all okay.

Ben, Many thanks for this code and your detailed instructions.

I've managed to use the login.php form to successfully log in and take me to the first page of my form. However when the form opens it doesn't load the data into the $fields array as I was hoping. I think this is because the array data loaded from login.php uses the field names taken from the database columns eg col_1, col_2, col_3 eg
Array ( [col_1] => John [col_2] => Doe [col_3]=> 34)

whereas the $fields aray uses field names from my form eg
Array ( [first_name] => John [last_name] => Doe [age]=> 34)

I think this is what you refer to in the above quote - is that right?

If so, how do I fix this? Do I need to manually change all the column titles in the database to match my form (a very long process!!) or is there a way to change the code of formtools so that when I first initialise my form it automatically uses the field name on my form for the column titles instead of col_x?

I hope I have made sense!
Thanks again
Reply
#7
Ah! Good point - I forgot about that.

Actually, it's really easy to map the form names to your database col names. Just log into Form Tools, edit your form and go to the Database tab. There, click on the "Smart Fill" button. That will rename all of your form fields to make them identical to your form fields. I think maybe I rely on that & kinda forgot that I did...

The only potential problem is if your form field names are MySQL reserved words. In that case, the JS "Smart Fill" option will append a prefix or suffix to the generated field name... I forget which.

Good grief, I'm forgetting everything today.

Anyway, give that a go. Smile

- Ben
Reply
#8
Hi Ben,

That's brilliant! That SmartFill Button works like magic.

And now to the next problem... (sorry!)

When I login I am taken to the form that I have previously submitted so that I can continue editing it (so far so good...) The problem is that because this form has already been submitted, when I submit the form again it returns error #302 - the form has already been finalized.

I think I have found the code in the api.php file that checks whether the form has been finalized. The code starts on line 616

PHP Code:
// check the submission ID isn't finalized
    
if (!$may_update_finalized_submissions && ft_check_submission_finalized($form_id$submission_id))
    {
          if (
$g_api_debug)
          {
            
$page_vars = array("message_type" => "error""error_code" => 302"error_type" => "user",
                
"debugging" => "{$LANG["phrase_submission_id"]}$submission_id");
              
ft_display_page("../../global/smarty/messages.tpl"$page_vars);
              exit;
          }
          else
            return array(
false302);
    } 
What I have is done is to delete this code and it seems to works fine. But I just want to check with you that I have done the right thing and it wont mess anything up!

Thanks again!
Reply
#9
Hey Speny,

Ah! That's fine - actually no, don't delete it. Instead, just add a new key-value pair to the $params you're passing to ft_api_process_form (the last row - ignore the other rows, they're just to show the context & are probably different for you):

PHP Code:
$params = array(
  
"submit_button" => "submit_button_name_attribute",
  
"next_page" => "next_page.php",
  
"form_data" => $_POST,
  
"no_sessions_url" => "first_page_in_form.php",
  
"may_update_finalized_submissions" => true
    
);
ft_api_process_form($params); 

The "may_update_finalized_submissions" setting will override the default behaviour & allow you to update a finalized submission. Hope that helps!

- Ben
Reply
#10
edited
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)