Quick links: Content - sections - sub sections
EN

Trace: goodies faq 1.0.3 1.1

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:tutorial:news-form [2007/01/14 09:17] doublefaceen:tutorial:news-form [2007/09/17 22:42] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Creation of a news form ====== +This page has been moved to [[en:tutorials:main:news-form]].
- +
-A form system, jForms, is under development and will be available in Jelix 1.0 beta2. jForms deals with received data from forms server side and client side at the same time (HTML, XUL, or web services) : creation, save, data check, etc. +
-This part is more or less operational in Jelix 1.0 beta1 but remains however experimental. jForms will be also handle the generation of HTML, XUL or other forms in the templates (starting from jelix 1.0 beta2). +
- +
-While waiting, you will have to use a more "traditional" method :-)  +
- +
-The goal of this chapter, will be to create a form in order to record some new news. +
- +
- +
-===== The template and the urls ===== +
- +
-Initially, we will make the template, very simplified, that we store in the newsform file :  +
- +
-<code xml> +
-<h1>Creation of a news</h1> +
- +
-<form action="{jurl 'news~default_createsave'}" method="POST"/> +
-<table> +
-<tr> +
-    <th><label for="subject">Subject</label></th> +
-    <td><input type="text" id="subject" name="subject" /></td> +
-</tr> +
-<tr> +
-    <th><label for="text">Text</label></th> +
-    <td><textarea id="text" name="text"></textarea></td> +
-</tr> +
-<tr> +
-    <th><label for="date">Date</label> (AAAA-MM-JJ)</th> +
-    <td><input type="text" id="date" name="date" /></td> +
-</tr> +
-</table> +
-<p><input type="submit" value="Save"/></p> +
-</form> +
-</code> +
-Very classical, put aside the template tag {jurl}. In Jelix, you will avoid putting urls directly in the templates or the actions, for ease of maintenance and evolution reasons. The URL system of Jelix enables you to centralize all the urls in the jelix config or a urls.xml file (all depends on the URL engine used). The jUrl object and the tag {jurl} enable you to obtain a URL by giving only the name of the action and eventually the parameters.  +
- +
-Here then, we state that the form will be submitted to URL of the default_createsave action. +
- +
-Note: by default, {jurl} correctly escapes the reserved characters in HTML/XML. +
- +
- +
- +
-===== Displaying the form ===== +
- +
-We will create a first action : "createform" to display an empty form for a new news. +
- +
-<code php> +
-    function createform(){ +
-        $rep = $this->getResponse('html'); +
-        $rep->title = 'New news'; +
-        $rep->bodyTpl = 'newsform'; +
-        return $rep; +
-    } +
-</code> +
- +
-To reach this page, we will add a link in bottom in the template listenews.tpl +
- +
-<code> +
-    <p><a href="{jurl 'news~default_createform'}">Add a news</a></p> +
-</code> +
- +
-Here, one more time, we use the {jurl} tag. +
- +
-===== Saving the data ===== +
- +
-As we stated in the form, we now have to create a "default_createsave" action to save the new data. +
- +
-Initially, we retrieve a record, that is filled with the data sent by the form. The URL parameters ($_GET) or posted ($_POST), are accessible via the param() method of the controllers. +
- +
-<code php> +
-        $news = jDao::createRecord('news~news'); +
-        $news->sujet = $this->param('subject'); +
-        $news->text = $this->param('text'); +
-        $news->news_date = $this->param('date'); +
-</code> +
- +
-We then retrieve a DAO factory to save the record. +
- +
-<code php> +
-        $dao = jDao::get('news~news'); +
-        $dao->insert($news); +
-</code> +
- +
-In the end, we will redirect to the list of news. +
-<code php> +
-        $rep = $this->getResponse('redirect'); +
-        $rep->action = 'news~default_index'; +
-        return $rep; +
-</code> +
- +
-Which gives finally : +
- +
-<code php> +
-    function createsave(){ +
-        $news = jDao::createRecord('news~news'); +
-        $news->subject = $this->param('subject'); +
-        $news->text = $this->param('text'); +
-        $news->news_date = $this->param('date'); +
- +
-        $dao = jDao::get('news~news'); +
-        $dao->insert($news); +
- +
-        $rep = $this->getResponse('redirect'); +
-        $rep->action = 'news~default_index'; +
-        return $rep; +
-    } +
-</code> +
- +
-You can now display the list of news one more time and reach the form. +
- +
----- +
-   * Next : [[en:tutorial:end|Next]] +
-   * Previous [[tutoriel:using-dao|Using a DAO]] +
-   * [[tutorial:|Back to the summary]] +

en/tutorial/news-form.1168766241.txt.gz · Last modified: 2007/09/17 22:42 (external edit)

Recent changes RSS feed Creative Commons License