PHP for Absolute Beginners by Thomas Blom Hansen & Jason Lengstorf

PHP for Absolute Beginners by Thomas Blom Hansen & Jason Lengstorf

Author:Thomas Blom Hansen & Jason Lengstorf
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


Creating the Entry Input Form

Now that you have a dynamic navigation, you might as well use it for something meaningful. You could show an HTML form for the editor. Eventually, it should be possible to use the editor to create new blog entries. So, the editor form should have fields for creating an entry title and an entry article. There should also be a button for saving a new entry. While you’re at it, you might as well create a button for deleting an entry. Create a new file views/admin/editor-html.php, as follows:

<?php

//complete source code for views/admin/editor-html.php

return "

<form method='post' action='admin.php?page=editor' id='editor'>

<fieldset>

<legend>New Entry Submission</legend>

<label>Title</label>

<input type='text' name='title' maxlength='150' />

<label>Entry</label>

<textarea name='entry'></textarea>

<fieldset id='editor-buttons'>

<input type='submit' name='action' value='save' />

<input type='submit' name='action' value='delete' />

</fieldset>

</fieldset>

</form>

";

Most of the preceding code should be familiar ground, even if you see a few elements you might not have come across before. You can see two <fieldset> elements. They are used to group related form fields together. The main <fieldset> has a <legend> element. A <legend> is like a heading for a <fieldset> element.

The <input> element for the entry title has a maxlength attribute set to 150. You can probably guess that the displayed text field will only accept 150 characters. That’s perfect, because your entry table in the database accepts a maximum of 150 characters for new title attributes.

The maxlength attribute enhances form usability, in that it becomes harder for users to create an invalid title through the form. The maxlength attribute performs client-side validation and will only allow submission of valid titles. One thing to keep in mind is the fact that client-side validation is great for enhancing usability. It does not improve security, because you should expect a malicious user to be able to override client-side validation.

With a new editor view created, you have to update the controller so it shows the view. The controller for the editor can be found in controller/admin/editor.php. Change the code so it is like the following:

<?php

//complete source code for controllers/admin/editor.php

$editorOutput = include_once "views/admin/editor-html.php";

return $editorOutput;

These few lines of code should hook up the view and display it when the controller is loaded from admin.php. You can see it for yourself by reloading http://localhost/blog/admin.php?page=editor in your browser. You should expect to see the form.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.