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.


<?php
  
# MySQL Parameters
  
$pmv_db_host="host";
  
$pmv_db_name="db";
  
$pmv_db_user="user";
  
$pmv_db_pass="pass";
  if (
file_exists('pmv_db_config.php')) {
    include(
'pmv_db_config.php');
  }

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

  
$pmv['edit'][0]['sql']='SELECT * FROM newsletter WHERE newsID="$msgid"';
  
$pmv['edit'][0]['main']='
        <form action="'
.$PHP_SELF.'" method="POST">
          <table> 
            <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>
          </table>
          <input type="hidden" name="pmv_action" value="update_entry">
          <input type="hidden" name="msgid" value="$newsID"><input type="submit" value="change">
        </form>'
;

  
$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>';
  
$pmv['update_entry'][0]['validate']='vali_newsletter.php';

  
# execute phpMyViews
  //$path_to_pmv=$_SERVER['DOCUMENT_ROOT']."/phpmyviews.php";
  
$path_to_pmv="phpmyviews.php";
  include 
$path_to_pmv;
?>

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


<?php
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.";
    }
    return 
$errors;
}
?>

Previous page
SourceForge.net Logo