user385729
user385729

Reputation: 1984

passing attributes of form_input by an array or dirrectly

I'm trying to add the placeholder and set_value for setting the value attribute by form_input of CodeIgniter. I tried the following code, but placeholder does not work:

 echo form_input('username', set_value('username'), "required='required'","placeholder='test'");

Am I passing the placeholder correctly?

Also, I tried to pass these attributes by an array by the following code:

$usernameData=array(
    'id'=>'username',
    'value'=>set_value('username'),
    'required'=>'required',
    'placeholder'=>'UserName',
);
    echo form_input($usernameData);

but for the above code set_value does not work! Could you please let me know how I can use set_value in an array and how if I dont use an array (first example), how can I get the placeholder work?

Please let me know if you need more clarification

Thanks

Upvotes: 0

Views: 4463

Answers (3)

Alex Florea
Alex Florea

Reputation: 79

Unfortunately I did not have time to test your code, but from the CodeIgniter Documentation http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#functionreference you need to set the name for the field

You must supply the field name via the first parameter of the function. The second (optional) parameter allows you to set a default value for the form

$usernameData=array(
'id'=>'username',
'name'=>'username', //you are missing this attribute
'value'=>set_value('username'),
'required'=>'required',
'placeholder'=>'UserName',
);

echo form_input($usernameData);

UPDATE

Form_input http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

Lets you generate a standard text input field. You can minimally pass the field name and value in the first and second parameter

If you don't pass an array, that means you can only add field_name and value as parameters to form_input.

echo form_input('username', 'johndoe'); 

If you would like your form to contain some additional data, like Javascript, you can pass it as a string in the third parameter:

$js = 'onClick="some_function()"';

echo form_input('username', 'johndoe', $js);

So, you should end up with:

$placeholder= 'placeholder="test"';
echo form_input('username', set_value('username'), $placeholder);

Upvotes: 4

Lalbhusan Yadav
Lalbhusan Yadav

Reputation: 111

you could use like this

<input name="country_name" id="country_name" value="<?php echo !empty($CountryDet)?$CountryDet['Country_Name']:'NA';?>" type="text" size="30" class="FormField" placeholder="Country Name" value="<?php echo set_value('country_name');?>" />

Upvotes: 0

Nithin Dev
Nithin Dev

Reputation: 421

Try this

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

Upvotes: 0

Related Questions