开发者

codeigniter - input form placeholder

Hey all, how开发者_高级运维 can I use the placeholder tag in CodeIgniter's form_input() helper function?

Thanks :)


Do you mean the placeholder attribute (not tag)? form_input() takes a third parameter with additional attributes.

$opts = 'placeholder="Username"';
form_input('username', '', $opts);

Or you can pass form_input() an array.

form_input(array(
  'name' => 'username',
  'value' => '',
  'placeholder' => 'Username',
));

CodeIgniter Form Helper


The Codeigniter user guide linked to by Rocket indicates that attributes other than name and value are passed to form_input() as an array:

 $data = array(
              'name'        => 'username',
              'id'          => 'username',
              'value'       => 'johndoe',
              'maxlength'   => '100',
  );

echo form_input($data);

// Would produce:

<input type="text" name="username" id="username" value="johndoe" maxlength="100" size="50" />

To expand on Rocket's answer, passing 'placeholder'=>'my_placeholder' into that array should produce the placeholder attribute.

$data = array(
              'name'        => 'username',
              'id'          => 'username',
              'value'       => 'johndoe',
              'maxlength'   => '100',
              'size'        => '50',
              'style'       => 'width:50%',
              'placeholder' => 'my_placeholder'
            );

echo form_input($data);

Keep in mind, the placeholder attr is very new and not supported in all browsers. Check out this article at html center for html5, jQuery, and pure javascript ways to accomplish placeholders


Codeigniter form placeholder for IE6, IE7 and IE8

echo form_input(array(
                'name' => 'stackoverflow',
                'value' => 'yourplaceholder',
                'placeholder' => 'yourplaceholder',
                'onclick' => 'if(this.value == \'yourplaceholder\') this.value = \'\'', //IE6 IE7 IE8
                'onblur' => 'if(this.value == \'\') this.value = \'yourplaceholder\''       //IE6 IE7 IE8
));


you can set placeholder like this

echo form_input('username','','placeholder=username');

this will look like this

<input type='text' name='username' placeholder='username'/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜