0
votes

i am having problem with redirection after the form validation. I Have 3 different forms in different views, after validation, if the form fails to submit i want to redirect it to the previous login page. it is working fine but, After submitting the form if i click on the url and press enter. when i am in homepage it redirects to contact. i want it to redirect it to the previous login page from which he submits. I think this is not the correct way of doing, thanks in advance

myform1.php My Form1

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>

myform2.php

<html>
<head>
<title>My Form</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email"  value="" size="50" />

<div><input type="submit" name-'submit2' value="Submit2" /></div>

</form>

</body>
</html>

myform3.php

<html>
<head>
<title>My Form3</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('con/valdate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<div><input type="submit" name="submit3" value="Submit3" /></div>

</form>

</body>
</html>

this is the controller page

   public function validate_form(){
                $this->load->library('form_validation');
                $this->form_validation->set_rules('firstname', 'First name', 'required');
if ($this->form_validation->run() == FALSE){
 ($this->input->post('submit1')=='submit1') ?  $this->load->view('myform1') 
: ($this->input->post('submit2')=='submit2') ?  $this->load->view('myform2') 
: $this->load->view('myform3'); 
}
else
{
$this->register();
}
}

controller con

    public function myform1(){
        $this->load->view('myform1');
    }

    public function myform2()   {
        $this->load->view('myform2');
    }

    public function myfrom3()   {
        $this->load->view('myform3');
    }
2

2 Answers

0
votes

I don't see why you have 3 same forms, but ok... why not. It can be a way to proceed (using the same validation method to avoid repeating code).

There are differents way to proceed:

1) Use a hidden input with the current location:

myform1.php

<?php echo validation_errors(); ?>

<?php echo form_open('con/validate_form'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />

<input type="hidden" name="redirect_to" value="<?php echo current_url(); ?>" />
<div><input type="submit" name='submit1' value="Submit1" /></div>

</form>

</body>
</html>

Controller > validate_form() method:

public function validate_form()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('firstname', 'First name', 'required');

    if ($this->form_validation->run() == FALSE)
    {
        $redirect_to = $this->input->post('redirect_to');
        redirect($redirect_to);
    }
    else
    {
        $this->register();
    }
}

But it's not perfect, because with redirect() method you can't easily re-populating the form.

2) This is why generally we don't use a validate_form().

You want to post a form, validate it, then show the form again with the validation errors if validation fails, or call register method if validation passes.

The best way to do this is to post a form back to itself. This way, in your method, you can check to see if the form was submitted, and determine what to do:

public function myform1()
{
    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('myform');
    }
    else
    {
        $this->register();
    }
}

The view:

<html>
<head>
<title>My Form 1</title>
</head>
<body>

<?php echo validation_errors(); ?>

<?php echo form_open('con/myform1'); ?>

<h5>Username</h5>
<input type="text" name="username" value="" size="50" />

<h5>Password</h5>
<input type="text" name="password" value="" size="50" />

<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />

<h5>Email Address</h5>
<input type="text" name="email"  value="" size="50" />

<div><input type="submit" name-'submit' /></div>

</form>

</body>
</html>
0
votes

what I do is I create a errors folder for different kind of form errors..... for form_validation errors I redirect it to the specific error page... It works fine for me....

I also feel you may be trying to use the same controller for multiple form views... That creates a problem.....

I think both your contacts and login form have the same redirect controller con/myform1.... check it out......

Hope that helps....