Welcome Guest, Not a member yet? Register   Sign In
need helping making POST data last for several page refreshes, so user can confirm data
#1

[eluser]Flying Fish[/eluser]
There may be a better way of going about this. I'm pushing the limits on my php skills here.

So here's the scenario, a user submits a valid form, then they review the data they submitted and can either 1. go back and make changes, or 2. confirm their data is correct

My questions are
1. I have all the users POST data for the confirmation page, but how do I repopulate the form if they choose to go back?
2. If they confirm their data, how do I write it all into the database?


Here is the controller.
Code:
function add_info()
    {
        // Check if the form has been submitted and passed validation
        // if it has show confirmation page
        if ($this->input->post('submit') && $this->form_validation->run() === TRUE)
        {
            $data['title'] = "Review Your Order";
            $data['heading'] = "Let's Review Your Order";
            
            $this->load->view('order_review', $data);
        } else if ($this->input->post('submit') && $this->form_validation->run() === FALSE)
        {
            // The form has been submitted and there are errors
            $data['title'] = "Oops! Let's double check that form.";
            $data['heading'] = "Oops! Let's double check that form.";
        } else
        {
            // The form has never been submitted, show the form
            $data['title'] = "Add Your Information";
            $data['heading'] = "Add Your Information";
        }
                
        // Load this view by default        
        $this->load->view('order_add_info', $data);
    }

Here's the view file for 'order_review'
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

    <h1>&lt;?=$heading?&gt;</h1>
    <p>Last but not least, please double check your info for accuracy.</p>
    <h2>Important</h2>
    <p>Please double check your info for mistakes! This will be exactly what is printed on your item.<p>
    <p>We can't be responsible for misspellings or typos.</p>
    <div id="review">
    <p>I want to order &lt;?=$_POST['order_quantity'];?&gt; items with the following info:</p>
    <h2>When</h2>
    <div><h3>Dates</h3>&lt;?=$_POST['when_dates'];?&gt;</div>
    <div><h3>Times</h3>&lt;?=$_POST['when_times'];?&gt;</div>
    <div><h3>Note</h3>&lt;?=$_POST['when_registration'];?&gt;</div>
    <div><h3>Extras</h3>&lt;?=$_POST['when_extra'];?&gt;</div>
    <h2>Who</h2>
    <div><h3>Ages</h3>&lt;?=$_POST['who_ages'];?&gt;</div>
    <div><h3>Extras</h3>&lt;?=$_POST['who_extra'];?&gt;</div>
    <h2>Where</h2>
    <div><h3>Church Name</h3>&lt;?=$_POST['where_name'];?&gt;</div>
    <div><h3>Address</h3>&lt;?=$_POST['where_street'];?&gt;</div>
    <div><h3>City</h3>&lt;?=$_POST['where_city'];?&gt;</div>
    <div><h3>State</h3>&lt;?=$_POST['where_state'];?&gt;</div>
    <div><h3>Zip</h3>&lt;?=$_POST['where_zip'];?&gt;</div>
    <div><h3>Phone(s)</h3>&lt;?=$_POST['where_phone'];?&gt;</div>
    <div><h3>Website</h3>&lt;?=$_POST['where_web'];?&gt;</div>
    <div><h3>Extras</h3>&lt;?=$_POST['where_extra'];?&gt;</div>
    <div>&lt;?=anchor('/order/add_info/'.'enter-var-here', 'I need to make some changes!');?&gt;</div>
    <div>&lt;?=anchor('/order/submit/', 'Everything looks ok.');?&gt;</div>
    </div>

&lt;/body&gt;
&lt;/html&gt;


I'm attaching the view 'order_add_info' as a zip file

Let me kno if I can clarify any of this. Thanks!
#2

[eluser]TheFuzzy0ne[/eluser]
You can store the previous pages in the form using hidden inputs. You can either use the values as the second parameter for set_value() or you could shuffle them about, like this:

Page 1
Code:
&lt;input type="text" name="field1" value="&lt;?php echo set_value('field1'); ?&gt;" /&gt;   &lt;!-- Active --&gt;
&lt;input type="hidden" name="field2" value="&lt;?php echo set_value('field2'); ?&gt;" /&gt; &lt;!-- Hidden --&gt;
&lt;input type="hidden" name="field3" value="&lt;?php echo set_value('field3'); ?&gt;" /&gt; &lt;!-- Hidden --&gt;

Page 2
Code:
&lt;input type="hidden" name="field1" value="&lt;?php echo set_value('field1'); ?&gt;" /&gt; &lt;!-- Hidden --&gt;
&lt;input type="text" name="field2" value="&lt;?php echo set_value('field2'); ?&gt;" /&gt;   &lt;!-- Active --&gt;
&lt;input type="hidden" name="field3" value="&lt;?php echo set_value('field3'); ?&gt;" /&gt; &lt;!-- Hidden --&gt;

Page 3
Code:
&lt;input type="hidden" name="field1" value="&lt;?php echo set_value('field1'); ?&gt;" /&gt; &lt;!-- Hidden --&gt;
&lt;input type="hidden" name="field2" value="&lt;?php echo set_value('field2'); ?&gt;" /&gt; &lt;!-- Hidden --&gt;
&lt;input type="text" name="field3" value="&lt;?php echo set_value('field3'); ?&gt;" /&gt;   &lt;!-- Active --&gt;


Hopefully this makes sense.

As the data will already be in the post array, this can be used to force validation of the data (to be sure the data is valid and hasn't been changed by the user), on a page that's being returned to.
#3

[eluser]JoostV[/eluser]
You could try to serialize the $_POST array, using serialize(). Serialize() converts your $_POST array into a string that you can store virtually anywhere.

You can then store this serialized POST array into flashdata.

On the new page, take the flashdata, unserialize it, using unserialize(), and use it to populate your form.

Haven't tested it, but it should work.
#4

[eluser]bretticus[/eluser]
You can always post the view to itself and just print the users responses above the same form if you detect post data. You would also just change the id/name on the submit button and handle it differently on the repost. Any of these ways mentioned will work though.

You may want to note that:

Code:
<div><h3>State</h3>&lt;?=$this->input->post('where_state', TRUE)?&gt;</div>

is safer than

Code:
<div><h3>State</h3>&lt;?=$_POST['where_state'];?&gt;</div>

If have no idea what you're building, and this may just be a registration form, but I am generally never fond of allowing content on my pages that have not been filtered for xss attacks.
#5

[eluser]vickel[/eluser]
I've just been into this (post data for several pages):

hidden input fields work fine (I'm not going to put somebodies data into my database, unless it's serious business), and I also use the form validation class @ http://ellislab.com/codeigniter/user-gui...ation.html

there is this very helpful concept:

controler:

Code:
$this->form_validation->set_rules('first_name', 'First Name', 'required|alpha_dash');

view: use the set_value() [or set_checkbox(), etc.] function to repopulate the form

hope this makes sense

Vickel
#6

[eluser]Flying Fish[/eluser]
Thanks all, I'm going to look at these options and try to implement one. I'll try to post back here when I get it working.
#7

[eluser]Flying Fish[/eluser]
One final post in case it helps anybody in the future. Hidden form fields was the the go for me!
#8

[eluser]simshaun[/eluser]
Depending on the situation, I usually use sessions.

Also, you don't have to serialize the POST array to put it in the session.




Theme © iAndrew 2016 - Forum software by © MyBB