Welcome Guest, Not a member yet? Register   Sign In
Form and form validation - Can't get it to work
#1

[eluser]vYN[/eluser]
Hi. I'am new to codeingiter and php and so on. And i got a problem that i can't fix..

So. here is the code:
function:
Code:
function register()
{
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('REGusername', 'Username', 'required|min_length[3]|max_length[35]|trim|callback__check_username');
$this->form_validation->set_rules('REGpassword', 'Password', 'required|min_length[4]|max_length[35]');
        $this->form_validation->set_rules('REGemail', 'Email', 'required');
      
    if ($this->form_validation->run() === FALSE)
        {
    $data['site_title'] = 'Register';
       $data['content'] = 'user/register';
    $this->load->view('templates/default', $data);
       }
       else
        {
  $this->user_model->registeruser();
        $data['site_title'] = 'Done';
        $data['content'] = 'user/done';
        $this->load->view('templates/default', $data);
        }
}

in the user_model:
Code:
function registeruser($REGusername, $REGemail, $REGpassword)
    {
        $time = time();
        $sha1_password = sha1($REGpassword);
        
        $query_str = "INSERT INTO users (username, email, password, time_registered) VALUES (?, ?, ?, ?)";
        
        $this->db->query($query_str, array($username, $email, $sha1_password, $time));
        
    }

When i try to fail the registration. it does not give me any errors on the page...

and i have "<?php echo validation_errors(); ?>" on the page.

And i got an another form also. And it is the same with that. I'm not sure if it is some other code. Because the other one is supposed to send a mail. It simply does not work/do anything when i hit the submit button....

and here is my website registration: http://stiantofte.net/user/register


EDIT:

I think there is something wrong with my form_validation... Because it seems it can't pass it anyway. or give errors and so on.
#2

[eluser]skunkbad[/eluser]
Please show view code, specifically the form.
#3

[eluser]vYN[/eluser]
Here it is:
Code:
<h1>Register!</h1><hr />
<p>Use the form below to regsiter!</p><br />
<div id="contact">
&lt;?php
echo form_open('user/register');

echo validation_errors();

echo form_label("Username: ", "username");
$data = array(
"name" => "REGusername",
"id" => "REGusername",
"value" => ""
);
echo form_input($data);

echo form_label("Password: ", "password");
$data = array(
"name" => "REGpassword",
"id" => "REGpassword",
"value" => "",
"type" => "password"
);
echo form_input($data);

echo form_label("Confirm password: ", "confirm password");
$data = array(
"name" => "REGcpassword",
"id" => "REGcpassword",
"value" => "",
"type" => "password"
);
echo form_input($data);

echo form_label("Email: ", "email");
$data = array(
"name" => "REGemail",
"id" => "REGemail",
"value" => ""
);
echo form_input($data);
echo form_submit ("registerSubmit", "Register!");

echo form_close();
?&gt;
</div>
#4

[eluser]Nisha S.[/eluser]
Remove the callback validation method callback__check_username and try. There might be an error with that function.
#5

[eluser]InsiteFX[/eluser]
This is wrong:
Code:
if ($this->form_validation->run() === FALSE)

// should be:
if ($this->form_validation->run() == FALSE)
#6

[eluser]vYN[/eluser]
[quote author="InsiteFX" date="1341310711"]This is wrong:
Code:
if ($this->form_validation->run() === FALSE)

// should be:
if ($this->form_validation->run() == FALSE)
[/quote]
ye i know. i have already fixed that. But still it does not work...

and i removed "callback__check_username" but that did also not work..
#7

[eluser]Nisha S.[/eluser]
@vYN "if ($this->form_validation->run() === FALSE)" is correct.
#8

[eluser]vYN[/eluser]
[quote author="Nisha S." date="1341337282"]@vYN "if ($this->form_validation->run() === FALSE)" is correct.[/quote]
well ok. But it still does not work...
#9

[eluser]vYN[/eluser]
i think there is a problem with the form. like it has problems opening the form or something. I'm not sure...
#10

[eluser]cartalot[/eluser]
ok here you call the model in the controller
Code:
$this->user_model->registeruser();

and here is the code in the model
Code:
function registeruser($REGusername, $REGemail, $REGpassword)

so first -- you need to take the values from the form, and pass them to registeruser. theres different ways to do this but take a look at the tutorial in the user guide for some first examples.

and then in the model, for registeruser - put in an error check, and for example return the id if true, and return false if the record did not insert
in model, in registeruser()
Code:
// if record inserted, kick back the ID, else false
if ( $this->db->affected_rows() == '1' )
{
$theid = $this->db->insert_id();
return $theid;
}
else {return FALSE;}

in controller
Code:
// IF registeruser did not create record
if ( ! $id = $this->user_model->registeruser($a,$b,$c) ) {
   $data['site_title'] = 'Register';
       $data['content'] = 'user/register';
    $this->load->view('templates/default', $data);
}
// else you can set $id in a session or whatever needs to happen next
else
{
// success
}




Theme © iAndrew 2016 - Forum software by © MyBB