[eluser]nir[/eluser]
Hello All,
In most cases the problems with form fields is that they are getting cached and not being cleared after successful submission in my case it's the opposite. when I hit the refresh button on the browser the form fields are getting cleared, also, If the form validation fails and you go back/redirected back to the form, again the form fields are getting cleared. -- I think that it somehow has to do with sessions but i can't figure out what is the problem. thanks in advanced.
~ nir
======================== CONTROLLER =====================================
Code:
<?php
class Registration extends Controller {
function Registration() {
parent::Controller();
session_start();
}
function index()
{
if(empty($_POST))
{
$this->load->model('MCaptcha');
$captcha = $this->MCaptcha->generateCaptcha();
$_SESSION['captchaWord'] = $captcha['word'];
$data['captcha'] = $captcha;
$data['title'] = 'Form';
$this->load->vars($data);
$this->load->view('registration.php',$data);
}
else
{
//testing
}
}
function register() //adding new user in the Database
{
//===========VALIDATION RULES===============
$rules['username']= "trim|required|xss_clean";
$rules['email']= "trim|required|xss_clean|valid_email";
$this->validation->set_rules($rules);
$fields['username'] = 'Username';
$fields['email'] = 'Email';
$this->validation->set_fields($fields);
if ($this->validation->run() == TRUE)
{
if(strcasecmp($_SESSION['captchaWord'],$_POST['captcha_input']) == 0)
{
$username = $this->input->post('username');
if(!$this->MUsers->getUserAccount($username))
{
if ($this->input->post('username')){
$this->MUsers->addUser();
$this->session->set_flashdata('message','User Created Successfully !');
redirect('application','refresh');
}
else
{
//testing
}
}
else
{
$this->session->set_flashdata('error','User Already Exist !');
$data['title'] = "Form";
$this->load->vars($data);
redirect('registration');
}
}
else
{
$this->session->set_flashdata('error','Characters Do Not Match !');
redirect('registration');
}
}
else
{
$data['title'] = "Error";
$this->load->vars($data);
$this->load->view('registration_form_error',$data);
}
}
}
?>
======================== END CONTROLLER =================================
======================== VIEW ===========================================
Code:
<?php $this->load->view('header'); ?>
<div id="content">
<?php
if ($this->session->flashdata('error')){
echo "<div class='message'>";
echo $this->session->flashdata('error');
echo "</div>";
}
?>
<div id="form">
<h3>Registration Form</h3>
<?php
//$pcdata = array('name'=>'passwordConfirm','id'=>'pc','size'=>35 );
$captchaData = array('name'=>'captcha_input','id'=>'captcha_input','size'=>35 );
echo form_open("registration/register");
echo "<p><label for='uname'><span class='starLabel'>* </span>Username : "."<br/>";
echo "<span class='smallLabel'>"."Usernames must be at least 4 characters long"."</span>"."</label>";
$data = array('name'=>'username','id'=>'username','size'=>35);
echo form_input($data)."</p>";
echo "<p><label for='email'><span class='starLabel'>* </span>Email Address: "."</label>";;
$data = array('name'=>'email','id'=>'email','size'=>35);
echo form_input($data)."</p>";
echo "<p><label for='p'><span class='starLabel'>* </span>Password : <br/>";
echo "<span class='smallLabel'>"."Passwords must be at least 5 characters long"."</span>"."</label>";
$data = array('name'=>'password','id'=>'password','size'=>35 );
echo form_password($data)."</p>";
echo "<p><label for='pc'><span class='starLabel'>* </span>Password Confirm : </label>";
$data = array('name'=>'passconf','id'=>'passconf','size'=>35 );
echo form_password($data)."</p>";
echo "<p style='height:50px;'>".$captcha['image']."</p>";
echo "<p><label for='Captcha'><span class='starLabel'>* </span>Type in the code: "."<br/>";
echo "<span class='smallLabel'>"."Type in the characters as it shows in the image above"."</span>"."</label>";
echo form_input($captchaData)."</p>";
echo "<div id='form-submitButton'>";
echo "<p style='border-bottom:none;'>".form_submit('submit','Submit')."</p>";
echo "</div>";
echo form_close();
?>
</div>
</div>
<?php $this->load->view('footer'); ?>