Haych
Haych

Reputation: 942

Formatting buttons in a form (CodeIgniter)

Just wondering how to go about giving a form in CodeIgniter a class? I've tried just formatting buttons, hoping that the submit button would change in the form, but it didn't.

echo form_open('User/feed' class='buttonClass');
echo form_submit('NF', 'News Feed');            
echo form_close();

I couldn't find anything which seemed to help me online.

Thank you!

Upvotes: 0

Views: 1975

Answers (2)

SpYk3HH
SpYk3HH

Reputation: 22570

echo form_open('User/feed', array( 'class' => 'classname' ));
// will become:
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/User/feed" class="classname" />


echo form_submit('NF', 'News Feed'); 
// will become:
<input type="submit" name="NF" value="News Feed" />


echo form_close();
// will become:
</form></div></div>

Now keep in mind, adding classes and other attributes via the array in the first line up there only adds them to the Form line. I would recomend, if you're doing this in view, writing pure html and adding in the information needed. More like:

<form method="post" accept-charset="utf-8" action="<?= base_url('User/feed'); ?>" class="classname">
    <div>
        Something here for the form
        <input type="text" name="stuff" />
    </div>
    <input type="submit" name="NF" value="News Feed" class="myButton" />
</form>

Also, of note, you can create buttons using an array and assign class and other attributes that way. Such as:

$myButton = array(
    'class' => 'myButton',
    'name' => 'NF',
    'value' => 'News Feed',
);
echo form_button($myButton);

Lastly, and I think this is what you're aiming for, you can do the same with form_submit:

$myButton = array(
    'class' => 'mySubmitButton',
    'name' => 'nfSubmit',
    'value' => 'Submit',
);
echo form_submit($data);

Upvotes: 1

Chris Baker
Chris Baker

Reputation: 50592

Per the documentation, the form helper accepts an array as the second parameter, allowing one to apply various options, such as a class. For example:

$attributes = array(
     'class'=>'myClass'
);
echo form_open('User/feed', $attributes);

Documentation

Upvotes: 1

Related Questions