This class is meant to generate forms dynamicly using javascript, this is very usefull to add more than one entry to database for example without reloading the page.
You can edit the generate function to add your own validation function and save date to db for example.
A sample usage is available at the end of the class.
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Hatem Ben Yacoub <info@phptunisie.net> |
// +----------------------------------------------------------------------+
//
// $Id: ddform.class.php,v 0.0.1 2004/12/19 11:07:31 hatem Exp $
/**
* Create and validate HTML forms dynamicly using Javascript & PHP
*
* @author Ben Yacoub Hatem <info@phptunisie.net>
*
*/
class ddform {
var $bgcolor1 = "#F5F6C4" ;
var $bgcolor2 = "#FCD6B0" ;
var $Fields = "" ; // Fields Will be saved here
var $Form = "" ; // Form will be saved here
var $Page = "" ; // Page will be saved here
/**
* @return ddform
* @desc Constructor
*/
function ddform () {
}
function version (){
return "0.0.1" ;
}
/**
* @return string
* @param string $name
* @param string $title
* @param string $type
* @param integer $size
* @param mixed $values
* @param string $default
* @param string $notes
* @param boolean $validation
* @desc Create new field : input, textarea, radio, checkbox ...
*/
function AddField ( $name , $title = "" , $type = "text" , $size = 30 , $values = "" , $default = "" , $notes = "" , $validation = false ){
switch ( $type ) {
case "radio" :
$notes = ( trim ( $notes )!= "" )? "\n<br><small> $notes </small>\n" : "" ;
$res = ' nHTML += \'<tr><th>' . $title . $notes . '</th><td>' ;
if ( is_array ( $values )) {
foreach ( $values as $k => $v ) {
$check = (isset( $v [ 'checked' ]))? " checked" : "" ;
$note = (isset( $v [ 'desc' ]))? $v [ 'desc' ]: "" ;
$res .= '<input type="radio" name="ddform[' . $name . '][\'+i+\']" value="' . $k . '" ' . $check . '/> ' . $note . ' ' ;
}
$res .= ' </td></tr>\';
' ;
} else {
if ( trim ( $default ) != "" ) {
$check = " checked" ;
} else $check = "" ;
$res .= '<input type="radio" name="ddform[' . $name . '][\'+i+\']" value="' . $values . '" ' . $check . '/> ' . $notes . ' </td></tr>\';
' ;
}
break;
case "checkbox" :
$notes = ( trim ( $notes )!= "" )? "\n<br><small> $notes </small>\n" : "" ;
$res = ' nHTML += \'<tr><th>' . $title . $notes . '</th><td>' ;
if ( is_array ( $values )) {
foreach ( $values as $k => $v ) {
$check = (isset( $v [ 'checked' ]))? " checked" : "" ;
$note = (isset( $v [ 'desc' ]))? $v [ 'desc' ]: "" ;
$res .= '<input type="radio" name="ddform[' . $name . '][\'+i+\']" value="' . $k . '" ' . $check . '/> ' . $note . ' ' ;
}
$res .= ' </td></tr>\';
' ;
} else {
if ( trim ( $default ) != "" ) {
$check = " checked" ;
} else $check = "" ;
$res .= '<input type="checkbox" name="ddform[' . $name . '][\'+i+\']" value="' . $values . '" ' . $check . '/> ' . $notes . ' </td></tr>\';
' ;
}
break;
case "textarea" :
$values = htmlentities ( $values );
$notes = ( trim ( $notes )!= "" )? "\n<br><small> $notes </small>\n" : "" ;
$res = ' nHTML += \'<tr><th>' . $title . $notes . '</th><td><textarea cols="40" rows="8" name="ddform[' . $name . '][\'+i+\']">' . $values . '</textarea> </td></tr>\';
' ;
break;
case "password" :
$values = htmlentities ( $values );
$notes = ( trim ( $notes )!= "" )? "\n<br><small> $notes </small>\n" : "" ;
$res = ' nHTML += \'<tr><th>' . $title . $notes . '</th><td><input type="password" size="' . $size . '" name="ddform[' . $name . '][\'+i+\']" value="' . $values . '"/> </td></tr>\';
' ;
break;
default:
$values = htmlentities ( $values );
$notes = ( trim ( $notes )!= "" )? "\n<br><small> $notes </small>\n" : "" ;
$res = ' nHTML += \'<tr><th>' . $title . $notes . '</th><td><input type="text" size="' . $size . '" name="ddform[' . $name . '][\'+i+\']" value="' . $values . '"/> </td></tr>\';
' ;
break;
}
$this -> Fields .= $res ;
return $res ;
}
/**
* @return string
* @param string $Fields
* @param string $name
* @param string $action
* @param string $method
* @param string $autocomplete
* @desc Create HTML DDForm
*/
function CreateForm ( $Fields = "" , $name = "ddform" , $action = "" , $method = "" , $autocomplete = "" ){
$Fields = ( trim ( $Fields )== "" )? $this -> Fields : "" ;
$form = "<!-- ddform form -->\n<form name=' $name ' " ;
$form .= ( trim ( $action )!= "" )? "action=' $action ' " : "" ;
$form .= ( trim ( $method )!= "" )? "method=' $method ' " : "method='post' " ;
$form .= ( trim ( $autocomplete )!= "" )? "autocomplete=' $autocomplete ' " : "" ;
$form .= ">\n" ;
$form .= "<div id=\"ddform_div\"></div>" ;
$form .= "<table bgcolor=white width=100% border=1 cellpadding=4 style=\"border-collapse: collapse\" bordercolor=#C0C0C0>
<tr><td><input type=\"button\" value=\"+\" onClick=\"AddField()\"> <small>Click on button <strong>+</strong> to add more entries</small></td></tr>\n
<tr><td align=right><input type=\"submit\" value=\"Save entry\"></td></tr>\n</table>\n" ;
$JScript = '<script language="javascript">
/**
* Create ddform Javascript
* Author : Ben Yacoub Hatem <info@phptunisie.net>
* License : PHP License
* Copyright PHPTunisie (C) 2004-2005 All rights reserved
*/
var i = 1;
window.onload = AddField();
function getFields(i)
{
var nHTML = "";
if (i%2 == 0) {
nHTML += \'<table bgcolor=' . $this -> bgcolor1 . ' width=100% border=1 cellpadding=4 style=\"border-collapse: collapse\" bordercolor=#C0C0C0>\';
} else {
nHTML += \'<table bgcolor=' . $this -> bgcolor2 . ' width=100% border=1 cellpadding=4 style=\"border-collapse: collapse\" bordercolor=#C0C0C0>\';
}
nHTML += \'<tr><td colspan=2>\'+i+\'</td></tr>\';
' . $Fields . '
nHTML += \'</table><HR align=left width=\"100%\" color=#aaaaaa noShade SIZE=1>\';
return nHTML;
}
function AddField()
{
if(document.all) var obj=document.all(\'ddform_div\');
else if(document.getElementById)
obj=document.getElementById(\'ddform_div\');
if(!obj) return;
var nHTML= getFields(i);
if(document.all)
obj.insertAdjacentHTML(\'beforeBegin\', \'<DIV class="ddform">\'+nHTML+\'<\/DIV>\');
else{
var nDIV=document.createElement(\'DIV\');
nDIV.innerHTML=nHTML;
nDIV.className="ddform";
obj.parentNode.insertBefore(nDIV, obj);
}
i++;
}
</script>' ;
$form .= $JScript ;
$form .= "</form>\n<!-- /ddform form -->\n" ;
$this -> Form = $form ;
return $form ;
}
/**
* @return string
* @param string $title
* @param string $notice
* @param string $content
* @param string $footer
* @desc Create HTML Page (optional)
*/
function CreatePage ( $title = "" , $notice = "" , $footer = "" , $content = "" ){
if ( $content == "" ) {
$content = $this -> Form ;
}
$page = "<!-- ddform page -->\n<center>\n" ;
if ( trim ( $title ) != "" ) {
$page .= "<h1 class=ddform_h1> $title </h1>\n" ;
}
if ( $notice != "" ) {
if ( is_array ( $notice )) {
$notice = "<ul><li>" . implode ( "<li>" , $notice ). "</ul>" ;
$page .= "<small> $notice </small>\n" ;
} else {
$page .= "<small> $notice </small>\n" ;
}
}
if ( trim ( $content ) != "" ) {
$page .= " $content \n" ;
}
if ( $footer != "" ) {
if ( is_array ( $footer )) {
$footer = "<ul><li>" . implode ( "<li>" , $footer ). "</ul>" ;
$page .= "<small> $footer </small>\n" ;
} else {
$page .= "<small> $footer </small>\n" ;
}
}
$page .= "</center>\n<!-- /ddform page -->\n" ;
$this -> Page = $page ;
return $page ;
}
/**
* return form and try to validate it.
*
*/
function generate (){
$content = ( trim ( $this -> Page )!= "" )? $this -> Page : $this -> Form ;
if (isset( $_POST [ 'ddform' ])) {
$keys = array_keys ( $_POST [ 'ddform' ]);
$size = sizeof ( $_POST [ 'ddform' ][ $keys [ 0 ]]);
$fields = implode ( ',' , $keys );
for ( $i = 1 ; $i <= $size ; $i ++) {
$values = array();
foreach ( $keys as $value ) {
$values [] = "'" . $_POST [ 'ddform' ][ $value ][ $i ]. "'" ;
}
$vals = implode ( "," , $values );
$query = "insert into table ( $fields ) values ( $vals );" ;
echo $query ;
}
return " $size elements added correctly into database." ;
} else {
return $content ;
}
}
}
?>
Usage Example
<?php
$f = new ddform ;
$f -> AddField ( "title" , "Title" , "text" , "40" );
$f -> AddField ( "resume" , "Resume" , "textarea" , "" , "Enter here <b>a resume</b>, it support HTML also." );
$f -> AddField ( "author" , "Author name" , "text" , "40" );
$f -> CreateForm ();
$f -> CreatePage ( "Add new article" , "Please fill in this form to create new article" , "All fields required." );
echo $f -> generate ();
?>
The Ajax Tree view class fetches data from a db for the requested parent category id. The data is then stored in an array and converted into JSON (Javascript Object Notation) format. This format is then used by JavaScript for populating tree view. Categories : PHP , PHP Classes , Java Script , AJAX , Databases Menu in sliding bar or tree style. Handles frames by using small amount of javascript. Handles external and internal pages. Allows custom code to replace a menu item. Categories : PHP Classes , PHP , Java Script , DHTML Db_lib - practical example usage of database abstraction and form validation.
Categories : PHP , Form Processing , PHP Classes , Data Validation , Beginner Guides Url To Pdf Report By Remote Application Categories : PHP , PHP Classes , PDF , CURL PHP4 DirectoryIterator Class Categories : PHP , PHP Classes , Filesystem , Directories Power Form Validation Categories : PHP , PHP Classes , Data Validation credit card security code Categories : PHP , Credit Cards , PHP Classes , Credit Cards Inline Scope Control - The inline class provides methods for creating and destroying local variable scopes. Simply put, local scopes are spaces where some or all of the global variables are temporarily hidden. Categories : PHP , PHP Classes , Variables TableMaint: class to help in the updating of data stored in database tables Categories : PHP , PHP Classes , Databases Linkers Class Categories : PHP Classes , PHP O-LOC : PHP 5.x Internationalization class and back-office. manage the display of translations on a localized web application. Categories : PHP , PHP Classes , Web Applications , Languages , DOM XML .htpassword manager for apache Categories : PHP , PHP Classes , Authentication , Apache Password using php, Javascript, and html form Categories : Security , PHP , Authentication , Java Script Pull Down Surfing - Surf on Change Categories : Java Script , MySQL , HTML and PHP , PHP , Databases Simple class that uses GD to draw pie charts. After the class definition there's some sample code to demonstrate how you use the class.
Categories : Graphics , PHP , PHP Classes , GD image library , Charts and Graphs
Raghavendra Joshi wrote : 1927
I was checking out this code sample as I am new to PHP. I
divided the code into 2 parts, ddform.php and
newddform.php. The ddform was the big file and the
newddform.php was the samaller usage example. When I try to
run the newddform.php, I get the following error
Fatal error: Class 'ddform' not found
in /nfs/c07/h03/mnt/110820/domains/resolv.org/html/applications/Raghu/newddform.php on line 2
Any Idea why this happens. Am I doing something wrong?