Quick links: Content - sections - sub sections
EN

Trace: news-form

A form system, jForms, is available in Jelix, but we will see it in a next chapter.

For the moment, we're going to see how to deal with forms in a “traditionnal” way. We will manipulate ourselves submitted data etc, and we will create an HTML 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 news/template/newsform.tpl file :

<h1>Creation of a news</h1>
 
<form action="{formurl 'news~default:createsave'}" method="POST">
{formurlparam}
<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"/>
<a href="{jurl 'news~default:index'}">Cancel</a></p>
</form>

Very classical, put aside the template tags {formurl}, {formurlparam} and {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 urls.xml file (or urls.xml files inside modules). 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.

So, for the “cancel” linkg, the generated url will be /index.php/news/default/index. If you move the index.php in an other directory, or if you change the url of the action into the urls.xml, you won't have to change your templates.

We could have used the tag {jurl} also in <form> tag, but if the URL contains parameters (as is the case here with module and action), it is best to put them in hidden fields. However, if we changed the configuration on the mapping url, it is not forced there is always these parameters. Also being used in conjunction plugins {formurl} and {formurlparam} who will themselves decide what to put in the attribute “action”, and what should be put in hidden fields.

Note: by default, {jurl}, {formurl} and {formurlparam} 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, into default.classic.php.

    function createform(){
        $rep = $this->getResponse('html');
        $rep->title = 'New news';
        $tpl = new jTpl();
        $rep->body->assign('MAIN', $tpl->fetch('newsform'));
        return $rep;
    }

To reach this page, we will add a link at the bottom of the template newlist.tpl

    <p><a href="{jurl 'news~default:createform'}">Add a news</a></p>

Here, one more time, we use the {jurl} tag.

Now refresh your browser at http://localhost:8080/index.php. You should see the new link. If you click on it, the form will be displayed.

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.

        $news = jDao::createRecord('news~news');
        $news->sujet = $this->param('subject');
        $news->text = $this->param('text');
        $news->news_date = $this->param('date');

We then retrieve a DAO factory to save the record.

        $dao = jDao::get('news~news');
        $dao->insert($news);

In the end, we will redirect to the list of news.

        $rep = $this->getResponse('redirect');
        $rep->action = 'news~default:index';
        return $rep;

Which gives finally :

    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;
    }

You can now display the list of news one more time and reach the form.

en/tutorials/jelixnews-1.7/news-form.txt · Last modified: 2019/05/23 09:51 by 127.0.0.1

Recent changes RSS feed Creative Commons License