Pages

Pages

Previous: Step 4: Adding instructions Up to Contents Next: Disabling unnecessary features

How to build submission forms using Xataface

Jump:

A submission form (a form for users to submit information into a database) is a very common use-case for Xataface. This tutorial teaches you how to do it the right way.

Creating a custom success page

Usually web forms will send the user to a success page with further instructions after the form is submitted.

Xataface allows you to attach triggers to various actions so that your PHP code is performed after the action is completed.  For some background on this feature, click here.

Creating the trigger

We will use the after_action_new trigger to forward users to our custom success page after the record is inserted by adding the following method to our table's delegate class.

/**
* Trigger that is called after new records are inserted. We will use it to
* forward to the correct page.
*/
function after_action_new(){
$auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser();

if ( !$user ){
// The user is not logged in so we forward to a success page.
header('Location: success.php');
exit;

}
}

The above method will automatically be called after the form is successfully processed and submitted.  It simply checks if a user is logged in.  If there is no user logged in, it will forward to success.php (our success page).  If the user is logged in, then this method does nothing, and the normal flow of control takes place.

Creating the success page

In our trigger above, we forwarded the user to a page called success.php.  We could simply create a page named success.php in our application's directory and place the success message there.  This would be fine.  However I would like to create the success page as a Xataface action so that it is more integrated into the framework.

This is a 2-step process:

  1. Add an entry to the actions.ini file as follows:
    [registration_successful]
    template=registration_successful.html
  2. Create a template called registration_successful.html in your templates directory with the following content:
    {use_macro file="Dataface_Main_Template.html"}
    {fill_slot name="main_section"}
    <h1>Registration Submitted Successfully</h1>
    <p>Thank you for registering for DiscoverFAS.
    We have received your submission and will be contacting you shortly with
    information about the event. In the mean time, if you have any questions,
    please contact us as <a href="mailto:{$ENV.APPLICATION.contact_email}">{$ENV.APPLICATION.contact_email}</a>.</p>

    <p>Please return to the <a href="http://fas.sfu.ca/DiscoverFAS">Discover FAS Web Page</a>.</p>
    {/fill_slot}
    {/use_macro}

    A couple of notes on the above template:

  3. At this point our success page will be accessible at the url http://yoursite.com/yourapp/admin.php?-action=registration_successful (because the action is named registration_successful - not because of the template's name).  However, our trigger above set up a forward to a page called success.php.  We either need to change the trigger to go to the correct URL, or we need to map success.php to our action so that requests to success.php will be forwarded to http://yoursite.com/yourapp/admin.php?-action=registration_successful.
    Let's take the latter route.
    Add the following line to your .htaccess file to accomplish this:
    RewriteEngine On
    RewriteRule success.php admin.php?-action=registration_successful [L]

We're done!  The form now redirects to a success page upon successful submission.

Now that the form is fully functional and secure, we can move onto the details...

Previous: Step 4: Adding instructions Up to Contents Next: Disabling unnecessary features
blog comments powered by Disqus
Powered by Xataface
(c) 2005-2024 All rights reserved