Welcome Guest, Not a member yet? Register   Sign In
form fields are being cleared when refrshing the web page
#1

[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">
&lt;?php
    if ($this->session->flashdata('error')){
        echo "<div class='message'>";
        echo $this->session->flashdata('error');
        echo "</div>";
    }
    ?&gt;
    <div id="form">
        <h3>Registration Form</h3>
        
        &lt;?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();
        ?&gt;
    </div>
</div>    

&lt;?php  $this->load->view('footer');  ?&gt;
#2

[eluser]jdfwarrior[/eluser]
Why do you echo all that HTML in your view?
#3

[eluser]nir[/eluser]
what's the alternative???
i am just starting my way in php.... codeigniter...
BTW this is how its being done in the book "Professional CodeIgniter"

thanks
#4

[eluser]jdfwarrior[/eluser]
Just use regular HTML and open the php where needed..

Code:
<div id="wrapper">
    <div id="container">

        <p>This is a standard paragraph</p>
        <p>This is a second standard paragraph</p>
        <p>This is a &lt;?=$variable?&gt; standard paragraph</p>

    </div>
</div>

That would use the php only where it was needed, in that one spot
#5

[eluser]nir[/eluser]
thank you, it does look more "clean".
any advice on the form fields no-caching after validation ???
thanks,
nir
#6

[eluser]TheFuzzy0ne[/eluser]
You need to set the value of the form fields using "set_value()". If you can post your controller, I can probably give you an example.
#7

[eluser]nir[/eluser]
i've attached the controller code and the view code at the top of this posting.
thank you,
nir
#8

[eluser]TheFuzzy0ne[/eluser]
Some general advice. CodeIgniter has a [url="http://ellislab.com/codeigniter/user-guide/libraries/sessions.html"]session library[/url] for you to use. It can make things a lot easier, I recommend you look into it. As you're using the session library anyway, it makes sense to use it to it's full potential.

CodeIgniter also has [url="http://ellislab.com/codeigniter/user-guide/libraries/input.html"]a cleaner way to access post variables[/url], so you don't have to keep checking if they are set or not (they return FALSE when they don't exist).

For repopulating the form, use set_value(). I see you're setting the form data in your view which I didn't notice, so here's an example of how to use it:
Code:
$data = array('name'=>'email','id'=>'email','size'=>35, 'value' => set_value('email'));
echo form_input($data)."</p>";
More information can be found in the [url="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html"]documentation for the form validation class[/url]. Perhaps I'm going blind at the ripe old age of 28, but I don't see where you're loading the form validation library. I assume it's set to load automatically?

EDIT: Another thing to note is that you do not need to call session_start(). The session library does all that for you.
#9

[eluser]nir[/eluser]
thanks for the quick response.
yes, i am auto loading the helpers and libraries in the config file.
still, i add your code change and i am having the same problem the the form fields are being cleared when i just refresh the web page or the validation fails, instead of keeping the values in the fields till a successful submission.
thanks again,
nir
#10

[eluser]jdfwarrior[/eluser]
Are you referring to like... you type a value in, and when you refresh it comes back?

Like, for a name field, I type in my name and hit refresh, and it keeps that value after the refresh?




Theme © iAndrew 2016 - Forum software by © MyBB