Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Would this be alright to use?

Options
  • 04-09-2012 7:37pm
    #1
    Registered Users Posts: 3,515 ✭✭✭


    Hey, so I am making websites quite a lot and using quite a lot of inputs, it's quite boring to write like 9 or 10 input form, so I came up with this function, do you think it's efficient and okay to use that way? or would this slow down website?
    <?php
    
    function Input($type, $name, $placeholder){
    
    if($type == "text" || $type == "password"){
    echo("<input type=\"".$type."\" name=\"".$name."\" placeholder=\"".$placeholder."\"/>");
    }
    
    
    else if($type == "submit"){
    if(isset($placeholder)){echo("<input type=\"".$type."\" name=\"".$name."\" value=\"".$placeholder."\"/>");}
    else{echo("<input type=\"".$type." name=\"".$name."\"/>");}
    }
    
    
    else if($type == "radio"){
    $values = explode(",", str_replace(" ", "", $placeholder));
    foreach($values as $value){
    echo("<input type=\"".$type."\" name=\"".$name."\" value=\"".strtolower($value)."\"/>".ucfirst($value)."<br/>");
    }
    }
    
    else if($type == "checkbox"){
    $values = explode(",", str_replace(" ", "", $placeholder));
    foreach($values as $value){
    echo("<input type=\"".$type."\" name=\"".$name."\" value=\"".strtolower($value)."\"/>".ucfirst($value)."<br/>");
    }
    }
    }
    
    ?>
    


    In main html file I wrote:

    [HTML]<?php
    include 'functions.php';
    ?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
    <title>Sample Reg</title>
    </head>
    <body>
    <form>
    <?php
    Input("text","username", "Username");
    Input("password","password","Password");
    echo("<br/>");
    Input("radio","number","one,two,three,four,five,six,seven");
    Input("checkbox","thing","red, black, blue, white, pink");
    Input("submit","submit","Send");
    ?>
    </form>
    </body>
    </html>
    [/HTML]

    And it worked perfectly. Think it's okay to use inputs that way?

    btw I know I didn't add method and action.


Comments

  • Registered Users Posts: 2,781 ✭✭✭amen


    Not a php guy but nothing wrong with that. A lot of frameworks do something similar.

    I wouldn't call a function after a keyword (Function Input vs html input) though so instead of Function Input I would maybe use Function AddControlToForm


  • Registered Users Posts: 3,515 ✭✭✭arleitiss


    amen wrote: »
    Not a php guy but nothing wrong with that. A lot of frameworks do something similar.

    I wouldn't call a function after a keyword (Function Input vs html input) though so instead of Function Input I would maybe use Function AddControlToForm

    Yeah that's just roughly for testing


  • Registered Users Posts: 26,571 ✭✭✭✭Creamy Goodness


    wouldn't slow down, what i would suggest though is to use classes, php is well known for just hacking bits together. We should use classes as much as possible.

    also i try to avoid escaping quotes in php strings if only for readability sakes and avoiding fencing (e.g. /\/\/\/\/\)

    [php]
    <?php
    class HtmlDecorator
    {
    /**
    * Creates an input html attribute
    *
    * @param string $type The type for this input
    * @param string $name The name for this input
    * @param string $value Optional, value of the input
    * @param string $classes Optional, classes to put into the tag
    * @param string $id Optional, an id for the tag
    *
    * @return string decorated input html.
    */
    public static function decorateInput(
    $type,
    $name,
    $value="",
    $classes="",
    $id=""
    ) {
    $class="";

    if (strlen($value) > 0)
    {
    $value = ' value="' . $value . '"';
    }
    if (strlen($classes) > 0)
    {
    $class = ' class="' . $classes . '"';
    }
    if (strlen($id) > 0)
    {
    $id = ' id="' . $id . '"';
    }

    return '<input type="' . $type . '" name="'
    . $name . '"' . $value . $id . $class . '/>';
    }
    }
    [/php]

    then use like the following:

    [php]
    HtmlDecorator::decorateInput('text', 'username', 'type your username here', 'userhandle', 'textinputclass');
    [/php]


Advertisement