2

So I'm going to be making a couple forms with multiple text input boxes, so I figured making a function to help automate this would be a good idea.

Below is the function I've come up with: however, the results I've gotten seem to be really weird, being a combination of "echo" and a mess of single quotes. Does everything look correct in there? I'm new to PHP, so I'm really sorry if it's an obvious mistake I'm missing.

    function makeTextInputField($name)
    {
echo '<label for = "<?php $name ?>"> <?php ucfirst($name) ?> </label><input type = "text" name = "<?php $name?>"></input>';
    }
0

8 Answers 8

3

You should not use any more tag inside the php

function makeTextInputField($name)
        {
    echo '<label for = "'.$name.'">'.ucfirst($name).'</label><input type = "text" name = "'.$name.'" />';
        }
Sign up to request clarification or add additional context in comments.

1 Comment

The input tag must be closed with this syntax: <input />. There is no need </input>.
1

Working Demo

Because you can insert line breaks in strings in PHP, you can make your function more readable by using variables inside it:

<?php

    function makeTextInputField($name) {
        $text = ucfirst($name);
        echo "
            <label for='{$name}'>{$text}</label>
            <input type='text' name='{$name}' />
        ";
    }

?>

And whenver you want to use it:

<h1>Welcome</h1>
<?php makeTextInputField('email'); ?>

OUTPUT

<h1>Welcome</h1>
<label for='email'>Email</label>
<input type='text' name='email' />

Comments

1

Your problem is that inside PHP code you're opening new PHP tags, which actually are not required. Try this function and see if it's working for you:

function makeTextInputField($name)
{
    echo sprintf('<label for="%s">%s</label> <input type="text" name="%s"></input>', $name, ucfirst($name), $name);
}

3 Comments

I guess people dont like sprintf. So sad. :(
echo sprintf() is the same as printf() :)
echo sprintf() is an "antipattern". There is absolutely no reason that anyone should ever write echo sprintf() in any code for any reason -- it should be printf() without echo every time.
0

Try with sprintf.

function textInput($name)
{
  $html = '<label for="%1$s">%2$s</label><input type="text" name="%1$s"/>';
  echo sprintf($html, $name, ucfirst($name));
}

1 Comment

echo sprintf() is an "antipattern". There is absolutely no reason that anyone should ever write echo sprintf() in any code for any reason -- it should be printf() without echo every time.
0
<?php

class DeInput
{
    protected $_format = '<div>
                 <label for="%s">%s</label>
                 <input  class="formfield" type="text"   name="%s"  value="%s">
                 </div>';
        public function render($content,$getFullyQualifiedName,$getValue,$getLabel)
    {

        $name = htmlentities($getFullyQualifiedName);
        $label = htmlentities($getLabel); 
        $value = htmlentities($getValue);
        $markup = sprintf($this->_format, $name, $label,  $name, $value);
        return $markup;
    }


}

Comments

0

Putting PHP code inside quotation marks is somewhat bad practice so I using (.) point to combine strings can be used.

Here is my example:

function makeTextInputField($name) {
    echo '<label for="'. $name .'">'.ucfirst($name).'</label>';
    echo '<input type="text" name="'.$name .' />';
}

Comments

0

use return intead of echo, and it will be easier to manipulate with result. Also you can split elements generation into different functions for more flexibility:

function createLabel($for,$labelText){
    return '<label for = "'.$for.'"> '.ucfirst($labelText).'</label>';
}

function createTextInput($name,$value,$id){
    return '<input type = "text" name = "'.$name.'" id="'.$id.'">'.$value.'</input>';
}

function myTextInput($name,$value,$labelText){
    $id = 'my_input_'.$name;
    return createLabel($id,$labelText).createTextInput($name,$value,$id);
}

echo myTextInput('email','','Type you email');

Comments

0
function makeTextInputField($name)
{
echo '<label for = "'.$name.'"> '.ucfirst($name).'</label><input type = "text" name = "'.$name.'"></input>';
 }

That should work.

You are already in php. So no need for the <?php tags. Concatenate strings together with a .

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.