• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Magic Form - Need Explanation

#1
[eluser]purpleparasol[/eluser]
I have coded two forms and please note **they are behaving as I would like**, but I don't know why. If I put the code here can someone explain why it is working.

I have a simple form on a view, with its own controller like this
Code:
<?=form_open('newslettersignup'); ?>
          <h2>Newsletter Signup</h2>
          <label>Name</label>
          &lt;input type="text" name="name" maxlength="25"&gt;&lt;/input>
          <label>Email</label>
          &lt;input type="text" name="email" maxlength="50"&gt;&lt;/input>
          <label>Country</label>
          &lt;?=$this->load->view("include-country-dropdown");?&gt;
          &lt;input class="button" type="submit" value="sign up"&gt;&lt;/input>
          &lt;?=form_close() ?&gt;

When someone fills in the values and clicks on the submit button, it goes to a different controller called newsletter signup which shows another form like the one below.

Code:
&lt;?=form_open('newslettersignup'); ?&gt;
   <h2>Newsletter Signup</h2>
   <label>Name</label>
   &lt;input value="&lt;?=set_value('name');?&gt;"  type="text" name="name"  maxlength="25"&gt;&lt;/input>
   <label>Email</label>
   &lt;input value="&lt;?=set_value('email');?&gt;" type="text" name="email" maxlength="50"&gt;&lt;/input>
   <label>Country</label>
   &lt;?=$this->load->view("publicuser/include-country-dropdown");?&gt;
   &lt;input class="button" type="submit" value="Send"&gt;
   &lt;?=form_close()?&gt;

The second form contains the values for name, email and country that the first form contained, which is what I wanted to happen. My question is - how did I do this? What is making the second form contain the same values as the first form when I don't think I've done anything explicitly to make this happen. Is it the set_value call?

Apologies if this sounds daft, but I've done what I want to do with no idea how I did it. Not really sure what set_value does when the values were in another, separate form.

#2
[eluser]GSV Sleeper Service[/eluser]
set_value is doing that. it will set the value to whatever is in the current $_POST, if the name exists. it also looks at the validation values too, neat! The user guide doesn't mention this at all...
from the source code -
Code:
/**
* Form Value - this needs updating too, should be 'Set Value'
*
* Grabs a value from the POST array for the specified field so you can
* re-populate an input field or textarea.  If Form Validation
* is active it retrieves the info from the validation class
*
* @access    public
* @param    string
* @return    mixed
*/
take a look at system/helpers/form_helper.php to see what's going on.

#3
[eluser]purpleparasol[/eluser]
Thank you - GSV Sleeper Service.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.