#1
  1. For POny!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    Amsterdam
    Posts
    416
    Rep Power
    115

    Sticky Form Class


    Hi guys,

    I had some time left so I thought I make a sticky form class, for fun and practice. Any code critiques are welcome. Hope it helps someone in this forum:

    PHP Code:
    <?php
    /*
     * Sticky form class 
     * aeternus | forums.devshed.com
     * 2013-02-25
     */

    class StickyForm {
        
    /*
         * @desc    returns the value of the input field sanitized and well
         * @param   $name   string
         * @param   $type   s,i representing either a string or integer
         * @return  type    mixed
         */

        
    public function getInputTextValue($name$flag 's') {
            
    //string
            
    if ($flag strtolower('s')) {
                
    $output = isset($_POST[$name]) ? htmlspecialchars($_POST[$name], ENT_QUOTES'UTF-8') : '';
            }
            
    //integer
            
    if ($flag == strtolower('i')) {
                
    $output = (int) $_POST[$name];
            }

            return 
    $output;
        }

        
    /**
         * @desc    return the textarea value sanitized and well
         * @param type $name    string
         * @return type string
         */
        
    public function getInputTextAreaValue($name) {

            
    $output = isset($_POST[$name]) ? htmlspecialchars($_POST[$name], ENT_QUOTES'UTF-8') : '';

            return 
    $output;
        }

        
    /**
         * @desc returns the radiobutton value and checks it sanitized and well
         * @param   type  $name     string
         * @param   type  $value    string
         * @return  type            string
         */
        
    public function getToggleValue($name$value) {

            if (isset(
    $_POST[$name]) && $_POST[$name] == $value) {

                
    $output htmlspecialchars($_POST[$name], ENT_QUOTES'UTF-8') . '" checked="checked';
            } else {
                
    $output $value;
            }

            return 
    $output;
        }

        
    /**
         * @desc returns the option value and selects it 
         * @param   type  $name     string
         * @param   type  $value    string
         * @return  type            string
         */
        
    public function getDropDownValue($name$value) {

            if (isset(
    $_POST[$name]) && $_POST[$name] == $value) {

                
    $output 'selected="selected"';
            } else {
                
    $output '';
            }

            return 
    $output;
        }

    }
    ?>
    example usage
    PHP Code:
    <!DOCTYPE html>
    <html>
        <head><meta charset="UTF-8" />
            <title>Sticky form class by aeternus  |  forums.devshed.com</title>
        </head>    
        <body>
            <h1>Sticky form class by aeternus  |  forums.devshed.com</h1>
    <?php
    $form 
    = new StickyForm();
    ?>
            <form action="" method="post">
                <div>
                    <ul style="line-height: 50px">

                        <li>
                            <span>example usage for an input textfield</span>
                            <input type="text" name="amount" value="<?php echo $form->getInputTextValue('amount'); ?>" />
                        </li>

                        <li>
                            <span>example usage for radiobuttons</span>
                            <span>a</span><input type="radio" name="monkey" value="<?php echo $form->getToggleValue('monkey''a'); ?>" />
                            <span>b</span><input type="radio" name="monkey" value="<?php echo $form->getToggleValue('monkey''b'); ?>" />
                        </li>
                        <li>
                            <span>example usage for a checkbox</span>
                            <input type="checkbox" name="checkbox" value="<?php echo $form->getToggleValue('checkbox''some value'); ?>" />
                        </li>
                        <li>
                            <span>example usage for a dropdown menu</span>
                            <select name="test">
                                <option <?php echo $form->getDropDownValue('test''bla'?>>bla</option>
                                <option <?php echo $form->getDropDownValue('test''bla2'?>>bla2</option>
                                <option <?php echo $form->getDropDownValue('test''bla3'?>>bla3</option>
                                <option <?php echo $form->getDropDownValue('test''bla4'?>>bla4</option>
                            </select>
                        </li>
                        <li>
                            <span>example usage for a textarea</span>
                            <textarea name="textarea"><?php echo $form->getInputTextAreaValue('textarea'); ?></textarea>
                        </li>
                    </ul>
                    <input type="submit" name="submit" value="submit" />
                </div>
            </form>
        </body>
    </html>
    Last edited by aeternus; February 24th, 2013 at 08:06 PM. Reason: added the textarea example :)

IMN logo majestic logo threadwatch logo seochat tools logo