form_open()

form_open([$action = ''[, $attributes = ''[, $hidden = array()]]])

Parameters:
  • $action (string) – Form action/target URI string
  • $attributes (array) – HTML attributes
  • $hidden (array) – An array of hidden fields’ definitions
Returns:

An HTML form opening tag

Return type:

string

Creates an opening form tag with a base URL built from your config preferences. It will optionally let you add form attributes and hidden input fields, and will always add the accept-charset attribute based on the charset value in your config file.

The main benefit of using this tag rather than hard coding your own HTML is that it permits your site to be more portable in the event your URLs ever change.

Here’s a simple example:

echo form_open('email/send');

The above example would create a form that points to your base URL plus the “email/send” URI segments, like this:

<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send">

Adding Attributes

Attributes can be added by passing an associative array to the second parameter, like this:

$attributes = array('class' => 'email', 'id' => 'myform');
echo form_open('email/send', $attributes);

Alternatively, you can specify the second parameter as a string:

echo form_open('email/send', 'class="email" id="myform"');

The above examples would create a form similar to this:

<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send" class="email" id="myform">

Adding Hidden Input Fields

Hidden fields can be added by passing an associative array to the third parameter, like this:

$hidden = array('username' => 'Joe', 'member_id' => '234');
echo form_open('email/send', '', $hidden);

You can skip the second parameter by passing any falsy value to it.

The above example would create a form similar to this:

<form method="post" accept-charset="utf-8" action="http://example.com/index.php/email/send">
        <input type="hidden" name="username" value="Joe" />
        <input type="hidden" name="member_id" value="234" />
doc_CodeIgniter
2016-10-15 16:32:20
Comments
Leave a Comment

Please login to continue.