Welcome Guest, Not a member yet? Register   Sign In
set_value() with redirect() issue
#11

[eluser]gRoberts[/eluser]
From what I have seen, the OP wants to get around the "resubmit" dialog that appears when pressing refresh etc.

If you are insisting on pursuing this, you would need to change your code to something like the following:

Code:
<?
if ($this->form_validation->run() === true)
{
    redirect('good_job');
}
else
{
  $this->load->library('session');
  foreach($_POST as $Key => $Value)
  {
   $this->session->set_flashdata($Key, $Value);
  }
  redirect();
}
?>

Then within your view, I would add

Code:
<?
$CI =& get_instance();
$CI->load->library('session');
?>

or load the library via autoload or via the action displaying the view.

then do:

Code:
Username : <input type="text" name="text_username" value="<?php echo $this->session->flashdata('text_username'); ?>" />
#12

[eluser]CroNiX[/eluser]
Wouldn't it be easier to just store the whole post array as a variable instead of looping through storing the values individually?

Code:
$this->session->set_flashdata('last_post', $_POST);
#13

[eluser]veledrom[/eluser]
Thank you very much to you all. Problem is solved by you teaching me some stuff.

METHOD
Code:
public function do_login()
{
   if ($this->form_validation->run() === true)
   {
      redirect('go_to_next_step');
   }
   else
   {
      $this->load->helper('redirect_form_validation_helper');
      redirect_form_validation(validation_errors(), $this->input->post(), 'login');
   }
}

MY HELPER
Code:
if (! function_exists('redirect_form_validation'))              
{
   function redirect_form_validation($form_errors, $form_post_data, $redirect_page)
   {
      $CI =& get_instance();
  
      $validation_errors = array('errors' => $form_errors, 'post_data' => $form_post_data);
  
      $CI->session->set_flashdata('validation_errors', $validation_errors);
  
      redirect($redirect_page);
   }
}


LOGIN PAGE
Code:
<?php
//** Print form validation errors
if ($this->session->flashdata('validation_errors'))
{
$validation_errors = $this->session->flashdata('validation_errors');

echo '<table id="table_validation_errors">';
echo '<tr><td>' . trim($validation_errors['errors']) . '</td></tr>';
echo '</table>';
echo '<br /><br />';

$text_username = isset($validation_errors['post_data']['text_username']) ? $validation_errors['post_data']['text_username'] : null;
}
?&gt;



&lt;?php echo form_open('login/do_login', array('name' => 'login', 'onsubmit' => 'return login_hash()')); ?&gt;

Username : &lt;input type="text" name="text_username" value="&lt;?php if(isset($text_username)) { echo $text_username; } ?&gt;" /&gt;
<br /><br />
Password : &lt;input type="password" name="text_password" value="" /&gt;
<br /><br /><br />
&lt;input type="hidden" name="hidden_form_hash" value="&lt;?php echo $form_hash; ?&gt;" /&gt;
&lt;input type="submit" name="submit_button" value="Login" /&gt;

&lt;?php echo '&lt;/form&gt;'; ?&gt;

#14

[eluser]veledrom[/eluser]
By the way, <b>set_flashdata()</b> is a milestone for my future development.
#15

[eluser]JulijanAndjelic[/eluser]
I know the topic is old, but since I ran into this same problem many times, so I decided to extend the native CI_Form_validation class.

Simply save the following code in application/libraries/MY_Form_validation.php

Code:
&lt;?php

class MY_Form_validation extends CI_Form_validation {

function __construct() {
  parent::__construct();
  $this->CI = &get;_instance();
  $this->CI->load->library('session');
  //store the posted data as soon as loaded
  foreach ($this->CI->input->post() as $k => $v) {
   $this->CI->session->set_flashdata('data_'.$k, $v);
  }
}

function run() {
  $valid = parent::run();
  if (! $valid) {
   $OBJ =& _get_validation_object();
   foreach ($this->CI->input->post() as $k => $v) {
    $this->CI->session->set_flashdata('error_'.$k, $OBJ->error($k, '', ''));
   }
  }
  
  return $valid;
}

}

?&gt;

then load the form validation class and do the validation just like you normally do:
Code:
$this->load->library('form_validation');
if ($this->form_validation->run() == FALSE) {
    redirect('myform');
}

What this will do is it will save all posted data and validation errors in a flashdata so it is available to you after redirect.

You can access the data using (in your views (form)):
Code:
$this->session->flashdata('data_field_name'); //replace field name with your actual field name

and to access form errors use:
Code:
$this->session->flashdata('error_field_name'); //replace field name with your actual field name

Needless too say that you must have the session class loaded so that it is available in your views.

Note: You don't need to load the form_validation library on the redirected page, all data is in flashdata!

Hope this helps someone.
#16

[eluser]Aken[/eluser]
Ideally you should not redirect when a form fails at all.
#17

[eluser]JulijanAndjelic[/eluser]
[quote author="Aken" date="1360355351"]Ideally you should not redirect when a form fails at all.[/quote]

I think you've missed the topic, this is the topic for us, who don't share your opinion and think it's okay to redirect after the failed form. I don't like writing the same code twice (loading the models, loading data for view, loading the views) and I also don't like users to be editing the form at the url which suggests something else eg. posts/save but instead to do it at posts/write.

My approach answers this topic and it does it in the OOP way.
Please explain why did you suggest that one shouldn't redirect after the failed form?
#18

[eluser]Aken[/eluser]
You don't have to write the same code twice to process a form. If you think you do, you're doing something wrong. If you have a form that shows up in multiple places (like a login form on every page, for example), the form's action should point to a single page, like example.com/login. If the login fails, then that form will reload at example.com/login, retaining the POST data.

Why would I suggest not redirecting after a failed form? Maybe for the exact reason that this thread exists in the first place -- the POST data disappears, and you have to add extra work-arounds to retain it after the redirect.

If the entire purpose of the redirect is to get the user back to the original page, then you can retain that data in their session info and bring them back upon successful form submission. That's the normal way of doing it.




Theme © iAndrew 2016 - Forum software by © MyBB