Welcome Guest, Not a member yet? Register   Sign In
Flash Data Without Redirect
#1

[eluser]RaGe10940[/eluser]
I am trying to send messages back to the users if a account does not exist. I have achieved this, however the data that the user typed in is gone and I spoke with the clients and they explicitly do not want that.

They want their information they typed in to save and with the code below :

Code:
$this->session->set_flashdata('emailview', 'Student Does Not Exist');
      redirect('emailsystem_controller/emailview', 'location');

it does not work...

I have tried this :

Code:
$this->session->set_flashdata('emailview', 'Student Does Not Exist');
     $this->emailview();

and that works only after I hit submit again (the next hop).

this is the code for emailview();

Code:
function emailview() {
$data['main_content'] = 'emailsystem/emails_view';
$this->load->view('includes/no_js/template', $data);
    }

So pretty much :

1) I need the flash data to be sent back with an error if the account does not exist.
2) I need the values the user inputted to not be reset

Is this possible?
#2

[eluser]boltsabre[/eluser]
Why don't you just use a form normally?

Basically you'd create a callback validation function function, which checks if the user exists, and if they don't display your validation message.

By using the inbuilt codeigniter form functions you can display your validation messge instead of trying to do it with flashsdata, and you can also populate the form data back into the inputs easily using the set_value() function.

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

#3

[eluser]RaGe10940[/eluser]
I'm not quite following you boltsabre

this is my code :

Code:
&lt;?php echo validation_errors('<p class="error">'); ?&gt;
&lt;?php echo $error ?&gt;
<p class="error">
    &lt;?php echo $this->session->flashdata('emailview'); ?&gt;
</p>
&lt;?php
$anum = 'placeholder="Student A-Number" autocomplete="off"';
$first = 'placeholder="Student First Name" autocomplete="off"';
$last = 'placeholder="Student Last Name" autocomplete="off"';
$email = 'placeholder="Student Email" autocomplete="off"';
$subject = 'placeholder="Email Subject" autocomplete="off"';
?&gt;
<br>
<div id="emails">
    <div id = "emailinfo">
<fieldset><legend>Find Student (Do Not Enter Email Bellow)</legend>
     &lt;?php
     echo form_open_multipart('emailsystem_controller/sendemail');
     echo form_input('anum', set_value('anum'), $anum);
     echo form_input('fname', set_value('fname'), $first);
     echo form_input('lname', set_value('lname'), $last);
     ?&gt;

</fieldset>
    </div>
    <div id="studentemail">
<fieldset><legend>Find Student Via Email</legend>

     &lt;?php
     echo form_input('email', set_value('email'), $email);
     ?&gt;
</fieldset>
    </div>
    <div id="subject">
<fieldset><legend>Subject</legend>
     &lt;?php
     echo form_input('subject', set_value('subject'), $subject);
     ?&gt;
</fieldset>
    </div>
    <div id="message">
<fieldset><legend>Email Message</legend>
     &lt;textarea name="message" rows="3" cols="60" placeholder="Email Message (Optional)"&gt;&lt;?php echo set_value('message') ?&gt;&lt;/textarea&gt;
</fieldset>
    </div>
    <div id="attachment">
&lt;input type="file" name="userfile" multiple="" /&gt;
    </div>
    <div id="sendemail">
&lt;?php
echo form_submit('submit', 'Send Email');
echo anchor('staff_controller/index', 'Return');
?&gt;
    </div>
</div>

<div id="footer">
    <p>
Special thanks to  <strong>Suny Orange Applied Science Department</strong> and <strong>SunyOrange Financial Aid Office </strong>
<br>
<strong>Application Created By Rixhers Ajazi</strong>
    </p>
</div>

what is it that you are trying to relay to me?

Thanks,
#4

[eluser]Otemu[/eluser]
Hi,

Basically boltsabre is saying that you should use Codeigniter Validation class

Provide the user with a form, when a user submits you can run a callback function(example in link), this would check if the user account exists, if it doesn't you can repopulate the form with the class. Saves you using flashdata and writing code for a feature that is already built within the frame work.

Hope that more clearer for you


#5

[eluser]boltsabre[/eluser]
In a simplistic overview of how userdata/flashdata and the global $_POST array works is:

- To display a newly created userdata/flashdata variable you have to use redirect().
- If you do this, your $_POST array gets emptied because well... there is nothing posted anymore, you've simple redirected them to a new script, which executes, thus the user hasn't had a chance to populate anything into $_POST.

So what you're trying to do, which is set & display a flashdata variable AND preserve the data inside $_POST, are at conflict with the above 2 points and cannot be done without some trickery.

The easiest way, and the path with the least resistance or chance of bugs, or data being lost, is to instead of using flashdata for this "Student Does Not Exist" validation check is to create a validation callback function.

I assume you are currently doing something like this???
- User submits form
- You validate it
- If errors are found you redisplay the form, with the users input still displayed in the inputs, and display the validation messages. Else
- Validation passed, now you do your model call to check if the student exists. If they don't exist you're trying to display the flash message with redirect() AND re-populate the form the the previous user input.

If so, don't... use a callback function. It's in the link I first posted. Are you checking the email address for if the student exists? If so, attach the callback to the email validation rules in your controller.
#6

[eluser]boltsabre[/eluser]
Ah we meet again Otemu, you beat me to it!!!
#7

[eluser]RaGe10940[/eluser]
Great community support I swear...

anywhos I got it situated and you guys gave me the idea... this is how I did it :

the error function in my controller :
Code:
else {
      $data['main_content'] = 'emailsystem/emails_view';
      $data['error'] = '<p class="error">Student Does Not Exist</p>';
      $this->load->view('includes/no_js/template', $data);
  }

view :

Code:
&lt;?php echo $error ?&gt;

works just fine. and now I can keep my input populated. This could be a "hacky" way of doing it but it works. I will take a look at the "other/right" way of doing it.

Again thank you to the both of you!
#8

[eluser]RaGe10940[/eluser]
And no,

I am using the Student ID, Student First, and Last, once the email is pulled from the DB, I decode the email (I use AES encrypt for all emails)

and then I use the email(s) that I decoded and then just have the $this->email->to($uncryptedemails);

works like a charm Big Grin

Again thank you to the both of you, this is a great community I must say.
#9

[eluser]boltsabre[/eluser]
No worries, glad you got it sorted out, but I'd certainly brush up on both "extending the validation library" and "custom callback functions" for form validation, you'll need it as soon as you need a form that can handle more than the basics.

And yes, it is a pretty great forum! One of the (many) reasons that I like... no, love... using CI!!!




Theme © iAndrew 2016 - Forum software by © MyBB