[eluser]Sally D[/eluser]
Hi first off thanks for making codeginiter I really am enjoying it.
Hi I am working on a proof of concept site and I am trying to make a login in page with form the form validation class. What I am having trouble doing is saving state like if the users name is already chosen or if they did not fill in the form correctly how can I get at the post data and populate the form fields that were filled in correctly so the user don't have to re type them?
I will post some code example
Here is my register controller
Code: function register()
{
$rules['username'] = "required";
$rules['password'] = "required";
$rules['first_name']= "required";
$this->validation->set_rules($rules);
if($this->validation->run() === false)
{
$title['title'] = 'Register';
$sidebar['title']='Registration Page';
$sidebar['desc'] ='Create an account';
$display['header'] = $this->load->view('header',$title, true);
$display['sidebar'] = $this->load->view('sidebar',$sidebar, true);
$display['body'] = $this->load->view('register','', true);
$display['footer'] = $this->load->view('footer','',true);
$this->load->view('main', $display);
}
else
{
$query = $this->db->getwhere('users', array('username' =>$this->input->post('username')));
if($query->num_rows() > 0) redirect('home/register/name_taken','refresh');
$this->db->set('username', $this->input->post('username'));
$this->db->set('password', $this->input->post('password'));
$this->db->set('first_name',$this->input->post('first_name'));
if($this->db->insert('users'))
{
/* here I am sending the user back to the log in page
and setting the 3rd segment in the url to trigger a message
on the log in page that they can log in with there username */
redirect('home/login/newuser','refresh');
}
}
Here is my registration form
Code: <div id="content">
<?
if($this->session->userdata('logged_in')) redirect('home','refresh');
$message = false;
?><h3>All Feilds Are Required!</h3> <?
?><?=$this->validation->error_string; ?><?
if($this->uri->segment(3) === 'name_taken') $message = 'sorry name taken please try again';
if($message){echo $message;}
echo form_open('/home/register/')."\n";
echo "Desired username<br />\n";
echo form_input(array('name'=>'username'))."<br />\n";
echo "Password<br />\n";
echo form_password(array('name'=>'password','value'=>$this->input->post('password')))."<br />\n";
echo "What's your first name<br />\n";
echo form_input(array('name'=>'first_name','value'=>$this->input->post('first_name')))."<br />\n";
echo form_submit('submit','Register')."\n";
echo form_close();
?>
What I would like to do is be able to get the post data so the user don't have to re type it if the make a mistake or something.
codeigniter really is nice its making me write code which I never did before I am just starting out thanks really
[eluser]JayTee[/eluser]
In the user guide, there's a section called "repopulating the form" in the validation class page:
Validation Class Docs
Specifically, it demonstrates how to use the set_fields method of the validation class to help maintain the values of the form.
[eluser]Sally D[/eluser]
I solved it The problem was with my logic
Now I would like to move on to form validation with ajax
here is my edited code
my registration veiw
Code: <div id="content">
<?
if($this->session->userdata('logged_in')) redirect('home','refresh');
?><h3>All Feilds Are Required!</h3> <?
?><?=$this->validation->error_string; ?><?
echo form_open('/home/register/')."\n";
echo "<h3>$error</h3>";
echo "Desired username<br />\n";
echo form_input(array('name'=>'username','value'=>$this->input->post('username')))."<br />\n";
echo "Password<br />\n";
echo form_password(array('name'=>'password','value'=>$this->input->post('password')))."<br />\n";
echo "What's your first name<br />\n";
echo form_input(array('name'=>'first_name','value'=>$this->input->post('first_name')))."<br />\n";
echo form_submit('submit','Register')."\n";
echo form_close();
?>
and now for the updated controller
Code: function register()
{
$rules['username'] = "required";
$rules['password'] = "required";
$rules['first_name']= "required";
$this->validation->set_rules($rules);
if($this->validation->run() === false)
{
$title['title'] = 'Register';
$sidebar['title']='Registration Page';
$sidebar['desc'] ='Create an account';
$display['header'] = $this->load->view('header',$title, true);
$display['sidebar'] = $this->load->view('sidebar',$sidebar, true);
$display['body'] = $this->load->view('register',array('error'=>''), true);
$display['footer'] = $this->load->view('footer','',true);
$this->load->view('main', $display);
}
else
{
$query = $this->db->getwhere('users', array('username' =>$this->input->post('username')));
if($query->num_rows() > 0)
{
$title['title'] = 'Register';
$sidebar['title']='Registration Page';
$sidebar['desc'] ='Create an account';
$display['header'] = $this->load->view('header',$title, true);
$display['sidebar'] = $this->load->view('sidebar',$sidebar, true);
$display['body'] = $this->load->view('register',array('error'=>'name taken'), true);
$display['footer'] = $this->load->view('footer','',true);
$this->load->view('main', $display);
}
else
{
//redirect('home/register/name_taken','refresh');
$this->db->set('username', $this->input->post('username'));
$this->db->set('password', $this->input->post('password'));
$this->db->set('first_name',$this->input->post('first_name'));
if($this->db->insert('users'))redirect('home/login/newuser','refresh');
}
}
}
My problem was that I was using a redirect and losing the post data there was a problem with my logic I thought it through now I would like to make a custom function then use the http request object to see if the user name is available before the user even post the form thanks
guys for making codeigniter again!
[eluser]Sally D[/eluser]
I take back all that code I just posted it's just no the CI way I read the manual and now I understand you got to use a callback function and instead use the validation class to re populate the fields on error thanks I under stand now
Some times you got to do things the hard way to truly understand the right way
Thanks for your help and suggestion here's my updated code I know you hate this but it's for my future reference
the new controller now using a callback function
Code: function register()
{
$rules['username'] = "callback_username_check";
$rules['password'] = "required";
$rules['first_name']= "required";
$this->validation->set_rules($rules);
$fields['username'] = 'Username';
$fields['password'] = 'Password';
$fields['first_name'] = 'first_name';
$this->validation->set_fields($fields);
if($this->validation->run() === false)
{
$title['title'] = 'Register';
$sidebar['title']='Registration Page';
$sidebar['desc'] ='Create an account';
$display['header'] = $this->load->view('header',$title, true);
$display['sidebar'] = $this->load->view('sidebar',$sidebar, true);
$display['body'] = $this->load->view('register','', true);
$display['footer'] = $this->load->view('footer','',true);
$this->load->view('main', $display);
}
else
{
$this->db->set('username', $this->input->post('username'));
$this->db->set('password', $this->input->post('password'));
$this->db->set('first_name',$this->input->post('first_name'));
if($this->db->insert('users'))
{
$this->session->set_userdata('username', $this->input->post('username'));
$this->session->set_userdata('password', $this->input->post('password'));
$this->session->set_userdata('first_name', $this->input->post('first_name'));
$this->session->set_userdata('logged_in',true);
redirect('home','refresh');
}
}
}
function username_check($user)
{
$query = $this->db->getwhere('users', array('username' =>$user));
if($query->num_rows() > 0)
{
$this->validation->set_message('username_check', 'The %s is already taken please try again');
return false;
}
else
{
return true;
}
}
And here is my view it's called register
Code: <div id="content">
<?
if($this->session->userdata('logged_in')) redirect('home','refresh');
?><h3>All Feilds Are Required!</h3> <?
?><?=$this->validation->error_string; ?><?
echo form_open('/home/register/')."\n";
echo "Desired username<br />\n";
echo form_input(array('name'=>'username','value'=>$this->validation->username))."<br />\n";
echo "Password<br />\n";
echo form_password(array('name'=>'password','value'=>$this->validation->password))."<br />\n";
echo "What's your first name<br />\n";
echo form_input(array('name'=>'first_name','value'=>$this->validation->first_name))."<br />\n";
echo form_submit('submit','Register')."\n";
echo form_close();
?>
That is so much easier other the instantiated the session I am tired and need sleep thanks man for helping me understand about that stuff now it's bed time
|