|
|
|
|
|
|
| |
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 | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | Authorize.net AIM Interface Class v1.0.0 Categories : PHP, PHP Classes, Ecommerce, Payment Gateways | | | crop and resize image class using gd library function Categories : PHP, PHP Classes, GD image library, Graphics | | | News management class Categories : PHP, PHP Classes, Beginner Guides | | | A Timing Class Categories : PHP, PHP Classes, Date Time | | | The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP, PHP Classes, Debugging | | | Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself. Categories : PHP, PHP Classes, Templates, Complete Programs | | | RSS parser.
Parses RSS into an array. Quick and nasty but does the job.
No checking is done for correct Tags, only correct XML.
PHP4 needed to display result (uses print_r). Categories : PHP, XML, PHP Classes, Rich Site Summary (RSS) | | | MS Word Mail Merge Automation (COM) Categories : PHP, PHP Classes, COM | | | MD5 secured login Categories : PHP, Java Script, Authentication, Security | | | Password Creator: This PHP code exmaple shows how to use bitwise operations on a single variable and using it as a flagged variable. The class generates passwords of a given length using specified characters and the flags. Categories : PHP, PHP Classes, Algorithms, Security | | | an example of the cyberlib payment class Categories : PHP, PHP Classes, Ecommerce, Credit Cards | | | MySQL Handler Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes | |
|
|
|