CodeIgniter Forums

Full Version: How to redirect on form validation succes?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]123wesweat[/eluser]
Hi,

Can't get a simple redirect to work

in controllers/welcome.php
i added a process function (which is called from

part of
Code:
function register() {
        $this->data = array(
                //comes from the db
                'pagetitle'    => "Tampa Business Directory",
                'page'              => "form-test"
        );
        $this->load->helper('url');
        $this->load->helper('form');

        $this->load->view('home_view',$this->data);
    }

function process() {
$this->load->helper('jsonwrapper');
        $this->load->helper('url');
        $this->load->library("validation");
....
...
if ($this->validation->run() == FALSE) {
            $data = array(
                    'fname' => $this->validation->fname_error,
                    'lname' => $this->validation->lname_error
            );
          
            echo json_encode($data);
        } else {
            $data = array(
                    'fname' => $this->input->post("fname"),
                    'lname' => $this->input->post("lname")
            );
            //put in our DB
            //print_r($data);
            //echo 'save data';// i see this in firebug
redirect('welcome');
          
        }

}

Or should i redirect to a view, like views/success.php?

I still see the form??

regards,

El Forum

[eluser]gigas10[/eluser]
Code:
function process() {
$this->load->helper('jsonwrapper');
        $this->load->helper('url');
        $this->load->library("validation");
....
...
if ($this->validation->run() == FALSE) {
            $data = array(
                    'fname' => $this->validation->fname_error,
                    'lname' => $this->validation->lname_error
            );
          
            echo json_encode($data);
        } else {
            $data = array(
                    'fname' => $this->input->post("fname"),
                    'lname' => $this->input->post("lname")
            );
            //put in our DB
            //print_r($data);
            //echo 'save data';// i see this in firebug
            redirect('welcome', 'location');
          
        }

}

I believe this will work, adding the 'location' parameter in the redirect function is the only thing I did.

El Forum

[eluser]123wesweat[/eluser]
hmm, tx but that didn't work.

in welcome.php i added
Code:
function success(){
        $this->load->helper('url');
        $this->load->view('success');
    }//.end success

so now i am testing with
Code:
function process() {
        $this->load->helper('jsonwrapper');//http://www.boutell.com/scripts/jsonwrapper.html
        $this->load->helper('url');
        $this->load->library("validation");

        $fields['fname'] = 'Fname';
        $fields['lname'] = 'Lname';
        $this->validation->set_fields($fields);

        $rules['fname'] = 'trim|required';
        $rules['lname'] = 'trim|required';
        $this->validation->set_rules($rules);

        $this->validation->set_message('required', '%s  is required');


        if ($this->validation->run() == FALSE) {
            $data = array(
                    'fname' => $this->validation->fname_error,
                    'lname' => $this->validation->lname_error
            );
          
            echo json_encode($data);
        } else {
           $data = array(
                    'fname' => $this->input->post("fname"),
                    'lname' => $this->input->post("lname")
            );
            //put in our DB
            //print_r($data);
           // echo 'save data';
           redirect('welcome/success', 'location');

        }
    }
but still i don't see success.

El Forum

[eluser]gigas10[/eluser]
hmm correct me if I'm wrong but isn't the validation class call form_validation?

Code:
function process() {
        $this->load->helper('jsonwrapper');//http://www.boutell.com/scripts/jsonwrapper.html
        $this->load->helper('url');
        $this->load->library("form_validation");

        $fields['fname'] = 'Fname';
        $fields['lname'] = 'Lname';
        $this->form_validation->set_fields($fields);

        $rules['fname'] = 'trim|required';
        $rules['lname'] = 'trim|required';
        $this->form_validation->set_rules($rules);

        $this->form_validation->set_message('required', '%s  is required');


        if ($this->form_validation->run() == FALSE) {
            $data = array(
                    'fname' => $this->validation->fname_error,
                    'lname' => $this->validation->lname_error
            );
          
            echo json_encode($data);
        } else {
           $data = array(
                    'fname' => $this->input->post("fname"),
                    'lname' => $this->input->post("lname")
            );
            //put in our DB
            //print_r($data);
           // echo 'save data';
           redirect('welcome/success', 'location');

        }
    }

I don't know if you redid the form_validation class or something, but at least that is what sticks out to me.

El Forum

[eluser]123wesweat[/eluser]
hmm, the validation part in combo with jquery works fine. And like i said if i put some text in both fields. It will validate and i see the save data with firebug.

- But the http request or CI redirect function isn't being executed.
- With firebug i do see the success.php source code as a post request
- I also see a get request but this doesn't load any response??

Maybe the way i am using the load->view is incorrect??

welcome/register: loads the form in a container, with
Code:
$this->data = array(
               'page'              => "form-test"
        );
$this->load->view('container',$this->data);

welcome/process: process the form and on success should load welcome/success

btw: i use libraries/validition and not libraries/Form_validation

El Forum

[eluser]123wesweat[/eluser]
hmm i think this could be part of my probllem

Code:
<?=form_open("/welcome/process", 'onsubmit="return false;" id="form"')?>

if i remove the return false; the form gets submitted when all fiels are not empty.

But now when one of the fields isn't empty the jquery errors don't work.

El Forum

[eluser]kgill[/eluser]
Quick question, since you're blocking submit in the form tag how's the form being submitted, is it via AJAX so that the process function is called in the background or do you do a submit via JS and it sends the browser to the welcome/process page?

El Forum

[eluser]Dyllon[/eluser]
It sounds like you're posting a form with AJAX so that request is in the background which means your trying to redirect the AJAX call.
If you wish to redirect after the form is submitted with AJAX you'll need to do it with the javascript.

Some more details on how you are submitting the form would probably clear up the confusion.

El Forum

[eluser]Dyllon[/eluser]
As a side note the validation class is deprecated as of CI 1.7.0

http://ellislab.com/codeigniter/user-gui...ation.html

El Forum

[eluser]123wesweat[/eluser]
ok, i have this demo.js
Code:
$(document).ready(function() {
    $("#form").submit(function() {
        var fname= $("#fname").val();
        var lname= $("#lname").val();
        $.post("/welcome/process", { fname:fname, lname:lname },
        function(data){
            $("#fname_error").html(data.fname);
            $("#lname_error").html(data.lname);
        },'json');
    });
});
and include
Code:
[removed][removed]
[removed][removed]

I got a bit lazy and i used http://formtorch.geekhut.org/

ps: i notice i am using the validation library but it's recommended to use form_validation
http://www.codeignitor.com/user_guide/li...ation.html
Pages: 1 2