[Release] Form Generation class.

Results 1 to 6 of 6
  1. #1
    Intelligent DoucheBag jur13n is offline
    MemberRank
    Jan 2008 Join Date
    Zwolle,Location
    1,946Posts

    [Release] Form Generation class.

    I saw the thread of qet and he created some "form generator" codes.
    it was horrible.

    so I created a form generation class.

    Preview
    Download

    Version 1:
    Spoiler:

    Usage:
    PHP Code:
    <?php
        
    //require the class
        
    require("require/Form_Generator.php");

        
    // First value = destination, second value = method, enctype is autoset. 
        
    $form = new form_generator("index.php?ThisIsTheDestination");

        
    // normal text boxes.
        
    $form->textbox("TextBoxname""TextBox Label""""TextBox Placeholder");
        
    $form->textbox("TextBoxname""TextBox Label""TextBox Value""");

        
    // break line
        
    $form->breakline("This is a break line!""red"); 

        
    // password textboxes
        
    $form->password("password""Password""""Your Password");  
        
    $form->password("rpassword""Repeat Password""""Repeat Your Password");
        
        
    // break line
        
    $form->breakline("Check if both passwords match!""blue");

        
    // create selections
        
    $form->select("selectionname""Selection Label""Choice1, Choice2, Choice3""chc1, chc2, chc3"); 

        
    // create radio buttons
        
    $form->radio("radioname""Radio Label""Choice1, Choice2, Choice3""chc1, chc2, chc3");

        
    // create checkboxes
        
    $form->checkbox("checkboxname""Checkbox Label""Choice1, Choice2, Choice3""chc1, chc2, chc3");

        
    // break line
        
    $form->breakline("Wasn't this usefull?""green");

        
    // file upload
        
    $form->file("NameofFile""File Label");

        
    //submit button
        
    $form->submit("Register");

        
    // Create the form.
        
    $form->buildForm();
    ?>
    Form_Generator.php class:
    PHP Code:
    <?php

        
    //////////////////////////////
        //                            //
        //         This class has     //
        //        been created by     //
        //         Jurien Hamaker     //
        //                            //
        // http://jurienhamaker.net //
        //                            //
        //        This is a free         //
        //           release             //
        //                            //
        //////////////////////////////

        
    class form_generator
        
    {

            var 
    $destination;
            var 
    $method;
            var 
    $enctype '';

            var 
    $fmBody '';
            var 
    $fmHeader;
            var 
    $fmFooter;
            var 
    $form;

            var 
    $choices;
            var 
    $names;
            var 
    $options '';

            public function 
    __construct($destination ''$method 'POST')
            {
                
    $this->destination $destination;
                
    $this->method $method;
            }

            public function 
    submit($value "submit"$name "")
            {
                
    $this->fmBody .= '<tr><td></td><td><input type="submit" value="'.$value.'" name="'.$name.'"></td></tr>';
            }

            public function 
    textbox($name$label ''$value ''$placeholder ''$color NULL)
            {
                
    $this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td><input id="'.$name.'" type="text" name="'.$name.'" value="'.$value.'" placeholder="'.$placeholder.'"></td></tr>';
            }

            public function 
    password($name$label ''$value ''$placeholder ''$color NULL)
            {
                
    $this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td><input id="'.$name.'" type="password" value="'.$value.'" placeholder="'.$placeholder.'" name="'.$name.'"></td></tr>';
            }

            public function 
    breakline($tekst NULL$color NULL)
            {
                
    $this->fmBody .= '<tr><td colspan="2" style="text-decoration:underline; padding-top:5px; padding-bottom:5px;"><center><font color="'.$color.'">'.$tekst.'</font></center></td></tr>';
            }
            
            public function 
    select($name$label ''$choices$names$color NULL)
            {
                
    $choices str_replace(" """$choices);
                
    $names str_replace(" """$names);

                
    $this->choices explode(","$choices);
                
    $this->names explode(","$names);

                
    $i 0;

                foreach(
    $this->choices as $choice)
                {
                    
    $this->options .= '<option value="'.$this->names[$i].'">'.$this->choices[$i].'</option>';
                    
    $i++;
                }

                
                
    $this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td>

                    <select id="'
    .$name.'" name="'.$name.'">
                    '
    .$this->options.'
                    </select>

                </td></tr>'
    ;
            }

            public function 
    radio($name$label ''$choices$names$color NULL)
            {
                
    $choices str_replace(" """$choices);
                
    $names str_replace(" """$names);
                
                
    $this->choices explode(","$choices);
                
    $this->names explode(","$names);

                
    $i 0;

                
    $this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td>';

                foreach(
    $this->choices as $choice)
                {
                    
    $this->fmBody .= '<input type="radio" name="'.$name.'" value="'.$this->names[$i].'"> '.$this->choices[$i].'<br>';
                    
    $i++;
                }
                
                
    $this->fmBody .= '</td></tr>';
            }

            public function 
    checkbox($name$label ''$choices$names$color NULL)
            {
                
    $choices str_replace(" """$choices);
                
    $names str_replace(" """$names);
                
                
    $this->choices explode(","$choices);
                
    $this->names explode(","$names);

                
    $i 0;

                
    $this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td>';

                foreach(
    $this->choices as $choice)
                {
                    
    $this->fmBody .= '<input type="checkbox" name="'.$name.'" value="'.$this->names[$i].'"> '.$this->choices[$i].'<br>';
                    
    $i++;
                }
                
                
    $this->fmBody .= '</td></tr>';
            }

            public function 
    file($name$label '')
            {
                
    $this->enctype "multipart/form-data";

                
    $this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td>
                    <input id="'
    .$name.'" type="file" name="'.$name.'">
                </td></tr>'
    ;
            }

            public function 
    buildForm()
            {
                
    $this->fmHeader '<table><form method="'.$this->method.'" action="'.$this->destination.'" enctype="'.$this->enctype.'">';
                
    $this->fmFooter '</form></table>';

                
    $this->form $this->fmHeader $this->fmBody $this->fmFooter;

                echo 
    $this->form;
            }


        }

        
    // end of class.
    ?>



    Version 2:
    Spoiler:

    Usage:
    PHP Code:
    <?php
        
    //require the class
        
    require("require/Form_Generator.php");

        
    // First value = destination, second value = method, enctype is autoset. 
        
    Form::Start("index.php?ThisIsTheDestination""POST"true);

        
    // normal text boxes.
        
    Form::textbox("TextBoxname""TextBox Label""""TextBox Placeholder");
        
    Form::textbox("TextBoxname""TextBox Label""TextBox Value""");

        
    // break line
        
    Form::breakline("This is a break line!""red"); 

        
    // password textboxes
        
    Form::password("password""Password""""Your Password");  
        
    Form::password("rpassword""Repeat Password""""Repeat Your Password");
        
        
    // break line
        
    Form::breakline("Check if both passwords match!""blue");

        
    // create selections
        
    Form::select("selectionname""Selection Label", array("Choice 1"=>"chc1""Choice 2"=>"chc2""Choice 3"=>"chc3")); 

        
    // create radio buttons
        
    Form::radio("radioname""Radio Label", array("Choice 1"=>"chc1""Choice 2"=>"chc2"));

        
    // create checkboxes
        
    Form::checkbox("checkboxname""Checkbox Label", array("Choice 1"=>"chc1""Choice 2"=>"chc2""Choice 3"=>"chc3""Choice 4"=>"chc4"));

        
    // break line
        
    Form::breakline("Wasn't this usefull?""green");

        
    // file upload
        
    Form::file("NameofFile""File Label");

        
    //submit button
        
    Form::submit("Register");

        
    // Create the form.
        
    Form::End();
    ?>
    Class:
    PHP Code:
    <?php

        
    //////////////////////////////
        //                            //
        //         This class has     //
        //        been created by     //
        //         Jurien Hamaker     //
        //                            //
        // http://jurienhamaker.net //
        //                            //
        //        This is a free         //
        //           release             //
        //                            //
        //////////////////////////////

        
    class Form
        
    {

            var 
    $destination;
            var 
    $method;
            var 
    $enctype '';

            var 
    $fmBody '';
            var 
    $fmHeader;
            var 
    $fmFooter;
            var 
    $form;

            var 
    $choices;
            var 
    $names;
            var 
    $options '';

            public function 
    start($destination ''$method 'POST'$enctype false)
            {
                if(
    $enctype == true)
                {
                    
    $enctype 'multipart/form-data';
                }

                echo 
    '<form method="'.$method.'" action="'.$destination.'" enctype="'.$enctype.'"><table>';
            }

            public function 
    submit($value "submit"$name "")
            {
                echo 
    '<tr><td></td><td><input type="submit" value="'.$value.'" name="'.$name.'"></td></tr>';
            }

            public function 
    textbox($name$label ''$value ''$placeholder ''$color NULL)
            {
                echo 
    '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td><input id="'.$name.'" type="text" name="'.$name.'" value="'.$value.'" placeholder="'.$placeholder.'"></td></tr>';
            }

            public function 
    password($name$label ''$value ''$placeholder ''$color NULL)
            {
                echo 
    '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td><input id="'.$name.'" type="password" value="'.$value.'" placeholder="'.$placeholder.'" name="'.$name.'"></td></tr>';
            }

            public function 
    breakline($tekst NULL$color NULL)
            {
                echo 
    '<tr><td colspan="2" style="text-decoration:underline; padding-top:5px; padding-bottom:5px;"><center><font color="'.$color.'">'.$tekst.'</font></center></td></tr>';
            }
            
            public function 
    select($sname$label ''$choices$color NULL)
            {
                
                echo 
    '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td>

                    <select id="'
    .$sname.'" name="'.$sname.'">
                    '
    ;

                    foreach(
    $choices as $name=>$value)
                    {
                        echo 
    '<option value="'.$value.'">'.$name.'</option>';
                    }

                echo 
    '</select>

                </td></tr>'
    ;
            }

            public function 
    radio($rname$label ''$choices$color NULL)
            {
                echo 
    '<tr><td><label for="'.$rname.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td>';

                foreach(
    $choices as $name=>$value)
                {
                    echo 
    '<input type="radio" name="'.$rname.'" value="'.$value.'"> '.$name.'<br>';
                }
                
                echo 
    '</td></tr>';
            }

            public function 
    checkbox($cname$label ''$choices$color NULL)
            {
            
                echo 
    '<tr><td><label for="'.$cname.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td>';

                foreach(
    $choices as $name=>$value)
                {
                    echo 
    '<input type="checkbox" name="'.$cname.'" value="'.$value.'"> '.$name.'<br>';
                }
                
                echo 
    '</td></tr>';
            }

            public function 
    file($name$label '')
            {
                echo 
    '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td>
                    <input id="'
    .$name.'" type="file" name="'.$name.'">
                </td></tr>'
    ;
            }

            public function 
    End()
            {
                echo 
    '</table></form>';
            }


        }

        
    // end of class.
    ?>
    Last edited by jur13n; 27-05-14 at 11:25 PM.


  2. #2
    Lurking around Clawed is offline
    MemberRank
    Jun 2012 Join Date
    RaGEZONELocation
    785Posts

    Re: [Release] Form Generation class.

    Instead of doing $choices, $names why not do 1 array?

    PHP Code:
    public function select($name$label ''$data = array(), $color NULL)
    {
        foreach(
    $data as $var => $value)
        {
            
    $this->options .= '<option value="'.$var.'">'.$this->value.'</option>';
        }
        
        
    $this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':&nbsp;&nbsp;</font></label></td><td>

            <select id="'
    .$name.'" name="'.$name.'">
            '
    .$this->options.'
            </select>

        </td></tr>'
    ;

    Then just:
    PHP Code:
    $form->select("Select""Select", array(=> "Rank 1"=> "Rank 2"), ""

  3. #3
    Intelligent DoucheBag jur13n is offline
    MemberRank
    Jan 2008 Join Date
    Zwolle,Location
    1,946Posts

    Re: [Release] Form Generation class.

    Quote Originally Posted by Clawed View Post
    Instead of doing $choices, $names why not do 1 array?

    PHP Code:
    public function select($name$label ''$data = array(), $color NULL)
    {
        foreach(
    $data as $var => $value)
        {
            
    $this->options .= '<option value="'.$var.'">'.$this->value.'</option>';
        }
        
        
    $this->fmBody .= '<tr><td><label for="'.$name.'"><font color="'.$color.'">'.$label.':  </font></label></td><td>

            <select id="'
    .$name.'" name="'.$name.'">
            '
    .$this->options.'
            </select>

        </td></tr>'
    ;

    Then just:
    PHP Code:
    $form->select("Select""Select", array(=> "Rank 1"=> "Rank 2"), ""
    I know it's more decent.
    Dave (he posts here frequently) told me too..

    but it's less user friendly for less skilled people.

  4. #4
    Intelligent DoucheBag jur13n is offline
    MemberRank
    Jan 2008 Join Date
    Zwolle,Location
    1,946Posts

    Re: [Release] Form Generation class.

    added version 2

  5. #5
    very nice B1QB0SS is offline
    MemberRank
    Jul 2013 Join Date
    518Posts

    Re: [Release] Form Generation class.

    Reupload

  6. #6
    Intelligent DoucheBag jur13n is offline
    MemberRank
    Jan 2008 Join Date
    Zwolle,Location
    1,946Posts

    Re: [Release] Form Generation class.

    Quote Originally Posted by B1QB0SS View Post
    Reupload
    Fixed
    http://snippets.jurienhamaker.net/sn...maker%20V2.rar



Advertisement