phpMyViews - Tutorial

Example 5 : Validation Example

phpMyViews also supports validation of form entries.

The following example has three actions:

  1. The 'default' action which renders a table with a list of news entries.
  2. The 'edit' action which allows editing one entry.
  3. And finally the 'update_entry' action which writes the changed values from the 'edit' action to the database.

The last step has an additional attribute 'validate'. A file name is given of a simple PHP script which can perform some validation of the entered form data.

# MySQL Parameters
  if (
file_exists('pmv_db_config.php')) {

# Actions
$pmv['default'][0]['sql']='SELECT newsID, newsDate, newsTopic FROM newsletter ORDER BY newsID DESC LIMIT 10';
        <form action="'
.$PHP_SELF.'" method="post">
          <input type="hidden" name="msgid" value="$newsID">
          <input type="submit" name="pmv_action" value="edit">

$pmv['edit'][0]['sql']='SELECT * FROM newsletter WHERE newsID="$msgid"';
        <form action="'
.$PHP_SELF.'" method="POST">
            <tr><td>Überschrift:</td><td><input type="text" name="topic" size="60" maxlength="120" value="$newsTopic"></td></tr>
            <tr><td>Text:</td><td><textarea name="text" rows="10" cols="50">$newsText</textarea></td></tr>
          <input type="hidden" name="pmv_action" value="update_entry">
          <input type="hidden" name="msgid" value="$newsID"><input type="submit" value="change">

$pmv['update_entry'][0]['sql']='UPDATE newsletter SET newsTopic="$topic", newsText="$text" WHERE newsID=$msgid';
$pmv['update_entry'][0]['post']='Done. <a href="'.$PHP_SELF.'">Back</a>';

# execute phpMyViews

Let's have a look now on the validation script. Here comes the source code for this script:

function vali_newsletter($values) {
$errors = array();
    if (
strlen($values['topic'])==0) {
$errors[] = "Topic must not be empty!";
    if (
strlen($values['text'])==0) {
$errors[] = "Text must not be empty!";
    if (
count($errors)>0) {
$errors[] = "Please go back and correct your inputs.";

Previous page Logo