Class To Make Forms Easily

Started on the old forum in February 2008 · view the original in the archive · old links redirect here

17 replies · 2,601 views · started 18 years ago · 1 min read

9 participants
Guest #1
<?php
/*
**Script Name: Form Maker class
**Purpose: A class to generate xhtml and wml forms code
**Script Written By: Ra'ed Shabanah 2008 (c)
**E-mail: [email]raed_mfs@yahoo.com[/email]
**COPYRIGHTS 

**This script was written fully by Ra'ed Shabanah, Senjil - Palestine
**use it as you wish,you can edit it.. etc 
*/
class Form
{
var $fields =**Array();
var $options= Array();
var $action;
var $method="post";
const TYPE_TEXT = 0;
const TYPE_PASS = 1;
const TYPE_SLCT = 2;
const TYPE_HIDN = 3;
const TYPE_SBMT = 4;
const TYPE_TXTA = 5; //text area
const XHTML = 0;
const WML = 1;

function addField($caption, $name, $type, $maxlength=0, $value="", $size=0)
{
$c = count($this->fields);
$this->fields[$c] = Array('caption'=>$caption, 'name'=>$name, 'type'=>$type, 'maxlength'=>$maxlength, 'value'=>$value, 'size'=>$size);
return $c;
}
function addOption($field, $name, $value, $xh)
{
$c = count($this->options);
$this->options[$c] = Array('field'=>$field, 'name'=>$name, 'value'=>$value, 'xh'=>$xh);
return $c;
}

function setAction($href)
{
$this->action=$href;
}
function setMethod($meth)
{
$this->method = $meth;
}
function getCode($output_type)
{
if($output_type==self::XHTML)
{
$code = '<form action="'.$this->action.'" method="'.$this->method.'">';
for($i=0;$i<count($this->fields); $i++)
{
switch($this->fields[$i]['type'])
{
case self::TYPE_TEXT:
$code .= $this->fields[$i]['caption'].': <input type="text" 
name="'.$this->fields[$i]['name'].'"
'.($this->fields[$i]['maxlength']>0?' maxlength="'.$this->fields[$i]['maxlength'].'"':'').'
'.($this->fields[$i]['size']>0?' size="'.$this->fields[$i]['size'].'"':'').'
'.(trim($this->fields[$i]['value'])!=""?' value="'.$this->fields[$i]['value'].'"':'').'
/>
';
break;
case self::TYPE_PASS:
$code .= $this->fields[$i]['caption'].': <input type="password" 
name="'.$this->fields[$i]['name'].'"
'.($this->fields[$i]['maxlength']>0?' maxlength="'.$this->fields[$i]['maxlength'].'"':'').'
'.($this->fields[$i]['size']>0?' size="'.$this->fields[$i]['size'].'"':'').'
'.(trim($this->fields[$i]['value'])!=""?' value="'.$this->fields[$i]['value'].'"':'').'/>
';
break;
case self::TYPE_TXTA:
$code .= $this->fields[$i]['caption'].'
 <textarea 
name="'.$this->fields[$i]['name'].'" 
cols="30" rows="5">
'.(trim($this->fields[$i]['value'])!=""?' value="'.$this->fields[$i]['value'].'"':'').'
</textarea>
';
break;
case self::TYPE_SBMT:
$code .='<input type="submit" name="'.$this->fields[$i]['name'].'" value="'.$this->fields[$i]['caption'].'" />

';
break;
case self::TYPE_SLCT:
$code .=$this->fields[$i]['caption'].': 
<select name="'.$this->fields[$i]['name'].'" value="'.$this->fields[$i]['value'].'" >
';
for($j=0; $j<count($this->options); $j++)
{
if($this->options[$j]['field']==$i)
{
$code .= '<option value="'.$this->options[$j]['value'].'" '.($this->options[$j]['xh']?($this->fields[$i]['value']==$this->options[$j]['value']?'selected="selected"':''):'').'>'.$this->options[$j]['name'].'</option>';
}
}
$code .= '</select>
';
break;
case self::TYPE_HIDN:
$code .= '<input type="hidden" 
name="'.$this->fields[$i]['name'].'" 
'.(trim($this->fields[$i]['value'])!=""?' value="'.$this->fields[$i]['value'].'"':'').'
/>';
break;
}
}
$code .= '</form>';
}else{
//wml code
$code ="";
$postfield ="";
for($i=0;$i<count($this->fields); $i++)
{
switch($this->fields[$i]['type'])
{
case self::TYPE_TEXT:
$code .= $this->fields[$i]['caption'].': <input 
name="'.$this->fields[$i]['name'].'"
'.($this->fields[$i]['maxlength']>0?' maxlength="'.$this->fields[$i]['maxlength'].'"':'').'
'.($this->fields[$i]['size']>0?' size="'.$this->fields[$i]['size'].'"':'').'
/>
';
$postfield .= '<postfield name="'.$this->fields[$i]['name'].'" value="$('.$this->fields[$i]['name'].')" />';
break;
case self::TYPE_PASS:
$code .= $this->fields[$i]['caption'].': <input type="password" 
name="'.$this->fields[$i]['name'].'"
'.($this->fields[$i]['maxlength']>0?' maxlength="'.$this->fields[$i]['maxlength'].'"':'').'
'.($this->fields[$i]['size']>0?' size="'.$this->fields[$i]['size'].'"':'').'
'.(trim($this->fields[$i]['value'])!=""?' value="'.$this->fields[$i]['value'].'"':'').'
/>
';
case self::TYPE_TXTA:
$code .= $this->fields[$i]['caption'].': <input 
name="'.$this->fields[$i]['name'].'"
'.($this->fields[$i]['maxlength']>0?' maxlength="'.$this->fields[$i]['maxlength'].'"':'').'.($this->fields[$i]['size']>0?' size="'.$this->fields[$i]['size'].'"':'').'
/>
';
$postfield .= '<postfield name="'.$this->fields[$i]['name'].'" value="$('.$this->fields[$i]['name'].')" />';
break;
case self::TYPE_SBMT:
$anchor ='
<anchor>'.$this->fields[$i]['caption'].'<go href="'.$this->action.'" method="'.$this->method.'">
<postfield name="'.$this->fields[$i]['name'].'" value="'.$this->fields[$i]['name'].'" />';
break;
case self::TYPE_SLCT:
$code .=$this->fields[$i]['caption'].': 
<select name="'.$this->fields[$i]['name'].'" value="'.$this->fields[$i]['value'].'" >';
for($j=0; $j<count($this->options); $j++)
{
if($this->options[$j]['field']==$i)
{
$code .= '<option value="'.$this->options[$j]['value'].'" '.($this->options[$j]['xh']?($this->fields[$i]['value']==$this->options[$j]['value']?'selected="selected"':''):'').'>'.$this->options[$j]['name'].'</option>';
}
}
$code .= '</select>';
$postfield .= '<postfield name="'.$this->fields[$i]['name'].'" value="$('.$this->fields[$i]['name'].')" />';
break;
case self::TYPE_HIDN:
$postfield .= '<postfield name="'.$this->fields[$i]['name'].'" value="'.$this->fields[$i]['value'].'" />';
break;
}
}
$code .= $anchor;
$code .= $postfield;
$code .= '</go></anchor>';
}

return $code;
}
}

?>

this class will make it much easier to make wap forms,

usage: simply put the above code in a file for example form.cls.php

i will post examples when i am back brb

Guest #3

this is a quick example:

//initialize the class
$myform = new Form();
//the action is where you want the information to be submited
$myform->setAction('index.php');
//the setMethod function is optional the default value is post, so if the form you are making will submit variables using the post method then you don't have the following line, else you need to use setMethod('get')
$myform->setMethod('post');
//now let's create our first field which is a simple input type="text" field
$myform->addField('Username', 'unm', Form::TYPE_TEXT, 15);
//password field
$myform->addField('Password', 'pwd', Form::TYPE_PASS, 15);
//now how to add a combo box
$login_type = $myform->addField('Login Type', 'lty', Form::TYPE_SLCT);
//add an option to the combo box
$myform->addOption($login_type,**'Visibe', 'v', true); //set to false if the form is wml form and not xhtml
//add another option
$myform->addOption($login_type,**'Invisible', 'i', true); //set to false if the form is wml form and not xhtml
//now let's add the submit button
$myform->addField('Login', 'snt', Form::TYPE_SBMT);
//now let's output the form
echo $myform->getCode(Form::XHTML); //use Form::WML if you are making a WML form

if you want to know more or want help or report bug tell meeeh External image (may be gone)

Guest #4

nice bro

Guest #5

Nice one External image (may be gone)

i was thinking since loong time to create something like this but never done it, thank you!

lol my pleasure i actually made this ages ago, but needed some finishings, plus i wanted to add more xhtml tags to it like input type="checkbox" and input type="radio" but i will do it later and also make it compitable with wml, but i decided i wil post this up for now and make improvements later

Guest #6

this is mines that i created

tags.php

<?php
/* tags.php */

/* Plagiarism Policy */
/* Ensure that you reference tags.php if you where to use it */
/* Wap: [url]http://xmobile.ws[/url] */

/* Feel free to edit and redistribute this */
/* Email: [email]postmaster@xpulse.net[/email] */

if(!defined('XFS'))
    die('Hacking attempt...');
    
    include_once("inc/func.php");
    include_once("inc/browser.php");
    include_once("config.php");
class tags
{
    public function head()
    {
        $browser = new browser;
        if($browser->check() == "xhtml")
        {
            header("Content-type: application/xhtml+xml; charset=UTF-8");
            header("Cache-Control: no-store, no-cache, must-revalidate");
        }
        if($browser->check() == "wml")
        {
            header("Content-type: text/vnd.wap.wml; charset=utf-8");
            header("Cache-Control: no-store, no-cache, must-revalidate");
        }
    }
    public function s_page()
    {
        $browser = new browser;
        if($browser->check() == "xhtml")
        {
            echo '<?xml version="1.0" encoding="UTF-8"?>';
            echo "\n";
            echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" ';
            echo "\n";
            echo '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
            

        }
        if($browser->check() == "wml")
        {
            echo '<?xml version="1.0" encoding="UTF-8"?>';
            echo "\n";
            echo '<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.2//EN" "http://www.wapforum.org/DTD/wml12.dtd">';
            echo "\n";
        }
    }
    public function pghead($title)
    {
        $browser = new browser;
        $func = new functions;
        global $theme_dir, $theme_url;
        if($browser->check() == "xhtml")
        {
               echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >';
            echo "\n";            
            echo '<head>';
            echo "\n";
            echo '<title>'.$title.'</title>';
            $theme = $func->theme();
            //echo '<link rel="StyleSheet" type="text/css" href="$theme_url/$theme/style.css" />';
            echo '<style type="text/css">';
            include_once("$theme_dir/$theme/style.css");
            echo '</style>';
            echo '</head>';
            echo "\n";
            echo '<body>';
            echo "\n";
        }
        if($browser->check() == "wml")
        {
            echo '<wml>';
            echo "\n";
            echo '<card id="index" title="'.$title.'">';
            echo "\n";
        }
    }
    public function pgfoot()
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo '</body>';
            echo '</html>';
        }
        if($browser->check() == "wml" )
        {
            echo '</card></wml>';
        }
    }
    public function p_open($align="left")
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "<div align=\"$align\">\n";
        }
        if($browser->check() == "wml" )
        {
            echo "<p align=\"$align\">\n";
        }
    }
    public function p_close()
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "</div>\n";
        }
        if($browser->check() == "wml" )
        {
            echo "</p>\n";
        }
        
    }
    public function src_break( $n = 1 )
    {
        echo str_repeat( "\n"  ,$n );
    }

    public function src_tab( $n = 1 )
    {
        echo str_repeat( "\t" , $n );
    }

    public function br($n = 1 )
    {
    
        echo str_repeat( "
" , $n );
        
    }
    public function add_image($image, $alt, $br=true)
    {
        global $theme, $theme_url;
        $browser = new browser;
        $func = new functions;
        $theme = $func->theme();
        if($browser->check() == "xhtml" )
        {
            echo "<img src=\"http://$theme_url/$theme/images/$image\" alt=\"$alt\"/> ";
            echo "\n";
        }
        if($browser->check() == "wml" )
        {
            echo "<img src=\"http://$theme_url/$theme/images/$image\" alt=\"$alt\"/> ";
            echo "\n";
        }
        if($br)
        {
            $this->br();
        }
    }
    public function hr()
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "<hr/>";
        }
        if($browser->check() == "wml" )
        {
            echo "
***
";
        }
            
    }
    public function add_text($text, $br=true)
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "$text";
            echo "\n";
            echo "\n";
        }
        if($browser->check() == "wml" )
        {
            echo "[size="1"]$text[/size]";
            echo "\n";
            echo "\n";
        }
        if($br)
        {
            $this->br();
        }    
    }
    public function add_link($link, $text, $br=true)
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "<a href=\"$link\">$text</a>";
            echo "\n";
        }
        if($browser->check() == "wml" )
        {
            echo "[size="1"]<a href=\"$link\">$text</a>[/size]";
            echo "\n";
        }
        if($br)
        {
            $this->br();
        }    
    }
    public function add_form($action, $method)
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "<form action=\"$action\" method=\"$method\">";
            echo "\n";
        }
        
    }
    public function add_hidden($name,$value)
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "<input type=\"hidden\" value=\"$value\" name=\"$name\"/>";
            echo "\n";
        }
        
    }
    public function close_form()
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "</form>";
            echo "\n";
        }
        if($browser->check() == "wml" )
        {
            echo "</go></anchor>";
            echo "\n";
        }
        
    }
    public function add_input($type, $name, $value="", $maxlength="100", $size=10, $mob=1)
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            if($mob=="1")
            {
                echo "<input type=\"$type\" name=\"$name\"/>";
                echo "\n";
            }else if($mob=="2")
            {
                echo "<input type=\"$type\" name=\"$name\" value=\"$value\"/>";
                echo "\n";
            }else if($mob=="3")
            {
                echo "<input type=\"$type\" name=\"$name\" value=\"$value\" size=\"$size\"/>";    
                echo "\n";
            }else if($mob=="4")
            {
                echo "<input type=\"$type\" name=\"$name\" value=\"$value\" size=\"$size\" maxlenght=\"$maxlength\"/>";
                echo "\n";
            }
        }
        if($browser->check() == "wml" )
        {
            if($mob=="1")
            {
                echo "<input type=\"$type\" name=\"$name\"/>";
                echo "\n";
            }else if($mob=="2")
            {
                echo "<input type=\"$type\" name=\"$name\" value=\"$value\"/>";
                echo "\n";
            }else if($mob=="3")
            {
                echo "<input type=\"$type\" name=\"$name\" value=\"$value\" size=\"$size\"/>";    
                echo "\n";
            }else if($mob=="4")
            {
                echo "<input type=\"$type\" name=\"$name\" value=\"$value\" size=\"$size\" maxlenght=\"$maxlength\"/>";
                echo "\n";
            }
        }
        
    }
    public function add_submit($title, $action="", $method="")
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "<input type=\"submit\" value=\"$title\"/>";
            echo "\n";
        }
        if($browser->check() == "wml" )
        {
            echo "<anchor>$title<go href=\"$action\" method=\"$method\">";
            echo "\n";
        }
    }
    public function post($name, $value)
    {
        $browser = new browser;
        if($browser->check() == "wml" )
        {
            echo "<postfield name=\"$name\" value=\"$value\" />";
            echo "\n";
        }
    }
    
    public function add_select($name)
    {
        echo '<select name="'.$name.'">';
        echo "\n";
    }
    public function close_select()
    {
        echo "</select>";
        echo "\n";
    }
    public function select_opt($value, $title)
    {
        echo "<option value=\"$value\">$title</option>";
        echo "\n";
    }
    public function add_home()
    {
    global $s, $o, $l, $lang;
        $this->br();
        $this->add_link("?l=$l&o=$o&s=$s","".$lang['home']."");
        echo "\n";
    }
    public function div($class)
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
            echo "<div class=\"$class\">";
            echo "\n";
        }
    }
    public function dive()
    {
        $browser = new browser;
        if($browser->check() == "xhtml" )
        {
                echo "</div>";
                echo "\n";
        }
    }
# end of oop #
}
Guest #7

simply example of a page

include_once("tags.php")

$tags = new tags;

$tags->head();
$tags->s_page();
$tags->pghead(Demo);
$tags->p_open();
echo "Demo";
$tags->p_close();
$tags->pgfoot();
Guest #8

U won't be able to use it if u don't have the browser.php file. here

class browser{
public function check()
{
// return xhtml
$this->browser = 'xhtml'; 
//Return wml
//$this->browser = 'wml'; 
}
//end of oop
}

You can create a auto detect browser and return xhtml or wml

Guest #10

You welcome!

Ghost Senior · 366 posts #11

very nice work thanks.. External image (may be gone)

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
rietrot Developer · 149 posts #13

ok im a noob what does tis actually do? or what can i do with it?

hebron Newbie · 3 posts #14

Its for display input form for both wml and xhtml.

pretend Developer · 107 posts #15

what input mate? and i tried it its saying hacking attempt and also doesnt have session included either, any way mate i wonder if i good test it every one says its nice and good no one helped to install it but. 🙁

Nice Effects

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close