CodeIgniter Forums
set_value() with redirect() issue - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: set_value() with redirect() issue (/thread-51690.html)

Pages: 1 2


set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]veledrom[/eluser]
Hi,

I cannot use set_value() in my forms because I'm using redirect(). Is there any way of using it along with redirect()?

echo set_value('text_username'); doesn't work.

Thanks


do_login() METHOD
Code:
if ($this->form_validation->run() === true)
{
   redirect('good_job');
}
else
{
   $this->session->set_userdata('validation_errors', validation_errors());
   //Stay in same page and print errors there
   redirect('login');
}

login PAGE
Code:
<h3>Login</h3>


&lt;?php
//** Print form validation errors
if ($this->session->userdata('validation_errors'))
{
   echo $this->session->userdata('validation_errors');
}
?&gt;



<div id="div_box">
&lt;?php echo form_open('login/do_login'); ?&gt;

Username : &lt;input type="text" name="text_username" value="&lt;?php echo set_value('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;
</div>



set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]the_unforgiven[/eluser]
Try changing your if statement around like so:

code]
if ($this->form_validation->run() == false)
{
$this->session->set_userdata('validation_errors', validation_errors());
//Stay in same page and print errors there
redirect();
}
else
{
redirect('good_job');
}
[/code]


set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]veledrom[/eluser]
No it didn't work.

NOTE: I know that I can use $this->login() but I need to use redirct().


set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]gRoberts[/eluser]
You would have to use flashdata and your own method of checking for a value and setting it, as the validation helper does not support you redirecting after validating.


set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]qpixo[/eluser]
[quote author="veledrom" date="1336999653"]No it didn't work.

NOTE: I know that I can use $this->login() but I need to use redirct().[/quote]

FYI, set_value() won't work while using redirect()

I had same problem before and someone helped me to fix it.
Basically you have to save the input form set_value() in session then load it into your input form

Code:
$this->session->set_userdata('your form', $this->form_validation->set_value('your form');

$this->session->userdata('your form');

Hope it helps!



set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]gRoberts[/eluser]
[quote author="qpixo" date="1337002209"][quote author="veledrom" date="1336999653"]No it didn't work.

NOTE: I know that I can use $this->login() but I need to use redirct().[/quote]

I had same problem before and someone helped me to fix it.

Basically you have to save the input form set_value() in session then load it into your input form

Code:
$this->session->set_userdata('your form', $this->form_validation->set_value('your form');

$this->session->userdata('your form');

Hope it helps!
[/quote]

As above, I'd use FlashData instead as they expire as soon as they are read, which means you don't want to worry about destroying or overlapping sessions.


set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]qpixo[/eluser]
[quote author="gRoberts" date="1337002290"][quote author="qpixo" date="1337002209"][quote author="veledrom" date="1336999653"]No it didn't work.

NOTE: I know that I can use $this->login() but I need to use redirct().[/quote]

I had same problem before and someone helped me to fix it.

Basically you have to save the input form set_value() in session then load it into your input form

Code:
$this->session->set_userdata('your form', $this->form_validation->set_value('your form');

$this->session->userdata('your form');

Hope it helps!
[/quote]

As above, I'd use FlashData instead as they expire as soon as they are read, which means you don't want to worry about destroying or overlapping sessions.[/quote]

Yeah sure! Ideally FlashData Smile
when user forget to Logout (doing all the process in background...)


set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]veledrom[/eluser]
How do I use flashdata in this case? I read it but didn't get it.


set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]boltsabre[/eluser]
Using "redirect" actually "loads" the new .php script, thus closing the old one - you're not extending a class, using "include/require" to load the .php script into your existing script or anything like that. As such, any variables instantiated in your first .php script are lost/deleted from memory - that's why you cannot access them, they no longer exist! If you want to keep them you need to hold them in memory by some storage mechanism (ie, cookies, sessions, DB)

Why do you even want to redirect if validation fails? Surely you just want to reload the same "view" and display your validation errors right? In that case, instead of doing a redirect, just load your view again

Code:
if ($this->form_validation->run() === true)
{
   redirect('good_job');
}
else
{
   //Stay in same page and print errors there
  $this->load->view('your_login_view_name', $data);
}



set_value() with redirect() issue - El Forum - 05-14-2012

[eluser]veledrom[/eluser]
As I said above ...

"NOTE: I know that I can use $this->login() but I need to use redirct()." which is same as $this->load->view('your_login_view_name', $data);