I am having trouble working with forms created using codeigniter. I have tried number of ways to render a form on the webpage. But am unsuccessful at different points.
I am looking to render this
I tried to render a form with code:
<?php $this -> load -> helper('form');
$formattr = array('id="form1"');
form_open('registerUsr', $formattr);?>
<fieldset>
<legend>Registration Form</legend>
<p class="first">
<label for="firstName">First Name: </label>
<input type="text" id="name" name="firstName" size="20" value="<?php echo set_value('first_name'); ?>" />
</p>
</fieldset>
<p class="submit"><button type="submit" value="Submit" /></p>
<?php echo form_close(); ?>
I tried to create it using codeigniter functions
<?php echo form_open('registerUsr');
echo form_input('$data');
echo form_submit('submit', 'Submit');
echo form_close();?>
I have tried hybrid variety using mix of html tags and helper class functions.
But in all the ways, on final render of the form on web page <form class=“form1”> tags are missing. And also instead of index.php/registerUsr, it is going into index.php/searchUniv page. (I have Checked routes config file. It is correctly pointed)
Any suggestions / ideas appreciated.
TIA 🙂
Update: Here is the controller and routes file
public function registration() {
$this -> template -> title -> set('Register');
$this -> template -> _content -> view('register_index', $overwrite = TRUE);
$this -> template -> _sidebar -> view('gen_sidebar', $overwrite = TRUE);
$this -> template -> publish();
}
public function registerUsr(){
$this -> template -> title -> set('Register');
$searchItem = $this -> input -> post('form1');
$this -> template -> publish();
}
}
register_index.php is the code thats in the question which now looks okay.
$route['searchUniv']="sglobal/searchUniv";
$route['registration']="sglobal/registration";
$route['registerUsr']="sglobal/registerUsr";
template is the custom library i wrote which is working absolutely fine in all other pages. This application is in final stages, so I can say with 95% confidence, template is working okay. Also I am looking at the source of final rendered page and its exactly whats wanted. So, Can we look into any configurations before pointing at template.
Now the final render looks like:
<form action="http://localhost/univapp/index.php/registerUsr" method="post" accept-charset="utf-8" id="form1" class="form1">
but on submit its going to http://localhost/univapp/index.php/searchUniv
JUST FYI, I also have another search form on the same page (far away div, different id, different name, different class) which uses POST that points to index.php/searchUniv. Is there any problem because of that?
Try to avoid using Codeigniter helpers if they are not needed, html renders faster than php. All that’s required from the form helper is form_open() as this adds the csrf token.
There also appears to be some typo’s in your code!
Checklist:
application/config/config.php
application/config/routes.php
views/registerUsr.php