Welcome Guest, Not a member yet? Register   Sign In
Multiline Form - Best Practices?
#1

[eluser]Kyle Johnson[/eluser]
Good Afternoon Everyone,

I am creating a form that will allow multiple line items to be added. Essentially, a web-based packing slip / order form.

I've run into many challenge along the way getting it to the point where the user can enter information, have it show up as a new line in a table.. BEFORE it is entered into the database for storage.

Currently I am storing most of the data within the database version of $this->session and then when the form is submitted for final processing I am using the session information as part of the information to enter into the DB. (I haven't started on the second part of putting it into the DB because I don't like how it's working yet).

I also have it setup in such a way that only a single item can be entered per page load. There is only one "item_id" dropdown, but when they hit the "Add Item" button, it is added to the session variable, and then the form is populated again.

What I am wondering is if you can think of a better way to do this. The way I am going about it seems to be causing a lot of extra coding that doesn't feel very natural with the flow of the rest of the code.

Any help or guidance would be appreciated.

Here is some of the code used. The first part is really messy...
Code:
if($this->input->post('add_item') && $this->input->post('company_master_model_id'))
        {
            if(!$this->session->userdata('contents'))
            {
                $session = array(
                                'contents' => array(
                                        array(
                                        'company_master_model_id' => $this->input->post('company_master_model_id'),
                                        'quantity' => $this->input->post('quantity'),
                                        'serial' => strtoupper($this->input->post('serial')),
                                        'asset' => strtoupper($this->input->post('asset')),
                                        'note' => $this->input->post('note')
                                        )
                            ));
            } else {
                $oldsession = $this->session->userdata('contents');
                $newsession = array(
                                        'company_master_model_id' => $this->input->post('company_master_model_id'),
                                        'quantity' => $this->input->post('quantity'),
                                        'serial' => strtoupper($this->input->post('serial')),
                                        'asset' => strtoupper($this->input->post('asset')),
                                        'note' => $this->input->post('note')
                                        );
                array_push($oldsession, $newsession);
                $session = array(
                                'contents' => $oldsession
                                );
            }
                
            $this->session->set_userdata($session);
        } else {
            if ($this->input->post('clear_item'))
            {
                $this->session->unset_userdata('contents');
            }
        }

Loop through the session and add to table of package contents. I use my own form library, but you can kinda interpolate what it does.

Company_master_model_id is a dropdown;
quantity, serial, asset, and note are all text fields;
add_item and clear_item are submit buttons.
Code:
$form->add_anything("<fieldset><legend>Package Contents</legend>");
        $form->add_anything('<div id="add_content">');
        $form->gen_input("company_master_model_id");
        $form->add_anything("<br />");
        $form->gen_input("quantity");
        $form->add_anything("<br />");
        $form->gen_input("serial");
        $form->add_anything("<br />");
        $form->gen_input("asset");
        $form->add_anything("<br />");
        $form->gen_input("note");
        $form->add_anything("<br />");
        $form->gen_input("add_item",'');
        $form->gen_input("clear_item",'');
        $form->add_anything("<hr />");
        $form->add_anything('</div>');
        $form->add_anything('<table width="100%"><tr><th style="width: 30px;">Line</th><th style="width: 250px;">Model</th><th style="width: 30px;">Qty</th><th>Serial</th><th>Asset</th><th>Note</th></tr>');
        if($this->session->userdata('contents'))
        {
            $line = 1;
            foreach($this->session->userdata('contents') as $val)
            {
                $form->add_anything('<tr>');
                $form->add_anything('<td>'.$line.'</td>');
                $i = 0;
                foreach($val as $v1)
                {
                    if($i == 0)
                    {
                        $form->add_anything('<td>'.$this->company_master_model->model_name($v1).'</td>'); // Get model name instead of showing just the ID
                    } else {
                        $form->add_anything('<td>'.$v1.'</td>');
                    }
                    $i++;
                }
                $form->add_anything('</tr>');
                $line++;
            }
        }
        $form->add_anything("</table>");
        $form->add_anything("</fieldset>");
#2

[eluser]TWP Marketing[/eluser]
If your form will have multiple items to send to the session, be aware of the size limit of 4Kb on the cookie data. You may be better off saving only the address information in the cookie and use a database to hold item entries.
#3

[eluser]Colin Williams[/eluser]
Quote:be aware of the size limit of 4Kb
That is moot if you use DB sessions.

Using the $form object, whatever that is, to add individual lines of HTML code is quite ridiculous. That can save you some ugly code right there. Keeping temp data in a session is an alright way to go about it, but you could also just write to the DB but have some sort of status/visibility field. Each field would get updated when the whole lot is committed/saved.
#4

[eluser]Kyle Johnson[/eluser]
Yes, I am using the DB Sessions support from within CodeIgniter, because I did originally run into the 4KB limit and it would only store about 5-6 line items. It was interesting.

The code is very messy, yes, and using the "$form->add_anything" function was a bit of a necessity during testing, I just have not yet gone back to the library to make things more reasonable as the page continues to evolve as people provide feedback. "Do this." "Change that." "I don't like the color." Love end users so much!

I thought about having it input into the database with a flag/visibility option like you explained, which would allow the user to actually save the information and return to it later (as it tracks what user is entering the information).

Any other possible ideas? Have any of you designed a form for a similar function where multiple line items were needed in a 1:N relationship? How did you go about solving the problem with the HTML forms?
#5

[eluser]Colin Williams[/eluser]
It's basically like a shopping cart, which I have built. There was a 'cart' table and 'cart_item' table. And in the session was the 'cart.cart_id' value.

You could probably look at the Cart library in SVN and get some ideas. As for the form on the front end, you can get fancy with JavaScript/Ajax and make it snappy, or you can just display a form under the current items, like commenting..
#6

[eluser]Kyle Johnson[/eluser]
Yeah!

I wanted to describe it as being like a shopping cart, but there is a lot more information that is captured other than, cart and items. I was hoping for an easy way to bypass having to create a new db schema. Though I guess modifying the tables I could just flag them with "in_progress (Y|N)" and just go from there.

I will take a look through the Cart library and see what they have in there that might help me out a bit.

I am not a fan of making the front end, so the snappy stuff might have to wait. It just gets so monotonous writing the same exact forms over and over.




Theme © iAndrew 2016 - Forum software by © MyBB