[ar] [ast] [bg] [br] [ca] [co] [cpf] [cs] [da] [de] [en] [eo] [es] [eu] [fa] [fon] [fr] [gl] [id] [it] [ja] [lb] [nl] [oc] [pl] [pt] [ro] [ru] [sk] [sv] [tr] [uk] [vi] [zh] Espace de traduction


CVT Forms by example

A contact form in 4 steps

August 2010 — updated on : May 2012

All the versions of this article:

Stage 1: Create the template for the form

In the sub-directory called formulaires/ of your "squelettes" folder, create a file called contact.html which will contain the HTML code for your contact form. For example:

  1. <form action='#ENV{action}' method='post'>
  3. <label>Enter your email</label>
  4. <input type='text' name='email' value='#ENV{email}' />
  5. <br />
  6. <label>Enter your message</label>
  7. <textarea name='message'>#ENV{message}</textarea>
  8. <input type='submit' name='ok' value='ok' />
  9. </form>


As you can see, this form only has a few constituent elements:

  • the action attribute for the <form> HTML tag contains #ENV{action}, which represents the URL of the current page on which the form will be sent
  • #ACTION_FORMULAIRE{#ENV{action}} tells SPIP to take into account the data sent by this form and the calls to the three functions: "charger, verifier, traiter"
  • each input, textarea or select field contains the #ENV{xxx} value, where xxx corresponds to the corresponding attribute name.

At this stage already, you can display this form in a template by using the #FORMULAIRE_CONTACT tag or insert it directly into the text of an article using what SPIP refers to as calling a "model" template with <formulaire|contact>. You are already able to test and refine the presentation and content of your form without needing to write even one single line of PHP code.

However, if you were to enter some text and actually click on the OK button, your form won’t do anything, and you won’t find your data going anywhere.

Stage 2: The "charger" function

So we’re now going to tell SPIP which fields the site visitor can enter.

To do this, create a contact.php file in the formulaires/ sub-directory (right beside your contact.html file), and put the following code into it:

  1. <?php
  3. function formulaires_contact_charger_dist(){
  4. $valeurs = array('email'=>'','message'=>'');
  6. return $valeurs;
  7. }
  9. ?>


Just what does this code do, then?

It declares the charger function for the contact form in the formulaires directory which is therefore named as formulaires_contact_charger.
The _dist suffix indicates that this is the default charger function for this form, but that it can be later customised (refer to the topic about overloading functions http://programmer.spip.org/Overload...).

This function lists in the $valeurs array variable all of the fields in your form, and the initial default value for each of those fields.
The formulaires_contact_charger_dist function then returns this list of fields.

Test your form again: if you enter the fields and validate with the OK button, you will see this time that the form has not lost the values that you have entered (even if the form still doesn’t actually do anything).

In this example, all of the fields are empty by default. We could improve our function by automatically filling out the email whenever the site visitor is identified.
This would give us the following code for the charger function:

  1. function formulaires_contact_charger_dist(){
  2. $valeurs = array('email'=>'','message'=>'');
  3. if ($GLOBALS['visiteur_session']['email'])
  4. $valeurs['email'] = $GLOBALS['visiteur_session']['email'];
  5. return $valeurs;
  6. }


If you now test your field once you have already been identified, you will notice that:

  • the email field has already been filled out with your SPIP account email
  • the values entered or modified are retained even after validation

Stage 3: Check that the entered data is correct

Before accepting the values entered by the site visitor, we should naturally check that whatever the visitor has entered is valid data.

So for example, we will define the following constraints:

  • the email and message fields are mandatory
  • the email field must be correctly formatted

To check the inputs, we will create the formulaires_contact_verifier_dist function (on a similar basis to that charger function) within the same contact.php file that we wrote before:

  1. function formulaires_contact_verifier_dist(){
  2. $erreurs = array();
  3. // check that mandatory fields are indeed filled out:
  4. foreach(array('email','message') as $obligatoire)
  5. if (!_request($obligatoire)) $erreurs[$obligatoire] = 'This field is mandatory';
  7. // check that any entered email address is correctly formatted:
  8. include_spip('inc/filtres');
  9. if (_request('email') AND !email_valide(_request('email')))
  10. $erreurs['email'] = 'This email address is not valid';
  12. if (count($erreurs))
  13. $erreurs['message_erreur'] = 'Your data entry contains errors!';
  14. return $erreurs;
  15. }


Note: using the code _request() is explained in detail on the site programmer.spip.org

The verifier function returns a list of fields in error, with the error message corresponding to each field that is in that list.

At this stage, you will not see any difference if you test the form: in fact, your contact.html form does not display any errors. You will therefore need to modify it as shown below:

  1. [<p class='formulaire_erreur'>(#ENV*{message_erreur})</p>]
  2. <form action='#ENV{action}' method='post'>
  4. <label>Enter your email</label>
  5. [<span class='erreur'>(#ENV**{erreurs}|table_valeur{email})</span>]
  6. <input type='text' name='email' value='#ENV{email}' />
  7. <br />
  8. <label>Enter your message</label>
  9. [<span class='erreur'>(#ENV**{erreurs}|table_valeur{message})</span>]
  10. <textarea name='message'>#ENV{message}</textarea>
  11. <input type='submit' name='ok' value='ok' />
  12. </form>



  • displays a general error message returned by the verifier function
    [<p class='reponse_formulaire reponse_formulaire_erreur'>(#ENV*{message_erreur})</p>]
  • the error messages before each field:
    [<span class='erreur_message'>(#ENV**{erreurs}|table_valeur{email})</span>]

You can now test your form by entering an invalid email address or by leaving any of the fields deliberately empty: your form is already interacting with the site visitor!

Stage 4: Do the form processing!

Whenever the verifier function does not return any errors, SPIP automatically called the corresponding traiter (process) function. We declare this function as formulaires_contact_traiter_dist (still in the same contact.php file) and make it send an email to the site’s webmaster:

  1. Our contact form';
  2. $message = _request('message');
  3. $envoyer_mail($email_to,$sujet,$message,$email_from);
  4. return array('message_ok'=>'Your message has been processed. You will receive a response as soon as possible!');
  5. }


Note that the verifier function does not do any checking: all the checking has been done previously (in the verifier function). If the traiter function is called, it means that the verifier function has already established that there are no major problems with the data entered.

The traiter function returns a success message, "message_ok" stored in an array. Just like the error messages, you need to modify the form so that this success message can be displayed, as shown in the code below:

  1. [<p class='formulaire_ok'>(#ENV*{message_ok})</p>]
  2. [<p class='formulaire_erreur'>(#ENV*{message_erreur})</p>]
  3. [(#EDITABLE|oui)
  4. <form action='#ENV{action}' method='post'>
  6. <label>Enter your email</label>
  7. [<span class='erreur'>(#ENV**{erreurs}|table_valeur{email})</span>]
  8. <input type='text' name='email' value='#ENV{email}' />
  9. <br />
  10. <label>Enter your message</label>
  11. [<span class='erreur'>(#ENV**{erreurs}|table_valeur{message})</span>]
  12. <textarea name='message'>#ENV{message}</textarea>
  13. <input type='submit' name='ok' value='ok' />
  14. </form>
  15. ]



  • displays a success message at the top of the form
    [<p class="formulaire_message">(#ENV*{message_ok})</p>]
  • conditionally displays the the data entry form in #EDITABLE mode: this means that after entering the data, it is generally more clear for the site visitor to not redisplay the whole form, but only the success message. That’s what #EDITABLE is used for.

And that’s it ! You can now deploy your contact form.


Would you like the interaction between your form and the site visitor to be faster and not need to completely reload the entire page any time that there’s an error or during the validation?

What you need is for your form to be run using the AJAX mode. Usually this is a cumbersome task, and this kind of implementation requires a lot of laborious and difficult work. But this has been simplified in SPIP, using the CVT standard methodology, which means that you can benefit from the automated systems available for forms, one of which includes ajaxification of your forms.

In order to turn it on, all you need to do is to enclose your form in a <div> with the class ajax:

  1. <div class='ajax'>
  3. </div>


And that’s all! Too easy by far...

To take things a little further

-  This simple example does not generate a terribly pretty form. For a more aesthetically pleasing, semantically useful and accessible result, do try and use the structural recommendations in The HTML structure of forms in SPIP for all of your SPIP forms.

-  Full details about the charger() function: The charger() function in CVT forms
-  Full details about the verifier() function: The verifier() function in CVT forms
-  Full details about the traiter() function: The traiter() function in CVT forms

Show the template of this page Site powered by SPIP | Translation area | Private area