Welcome Guest, Not a member yet? Register   Sign In
Pull record based on dropdown selection
#1

[eluser]QuestionBoy[/eluser]
I am brand new to CI And am just starting to wrap my head around the frameworks and the power it can offer.

I previously created web apps using pure PHP. In my main form there was a drop-down that the use could make a selection and upon posting the PHP would pull up that record for viewing/editing.

I am now trying to do the same using CI and am lost. I don't know if there is a term for this because I have been unable to locate a tutorial for this type of thing. Yet, I am most certainly not the first person to do this.

Can anyone offer some guidance, or perhaps a link to a tutorial that covers this. I was thinking it would be best to use AJAX or JSON instead of reloading the page, but perhaps someone here can offer better advice.

Thank you for your patience and guidance. It truly is greatly appreciated!

QuestionBoy
#2

[eluser]InsiteFX[/eluser]
What does the selection contain for getting the record?

Usally you just do you database method and use ci input to grab the form data.
#3

[eluser]QuestionBoy[/eluser]
It should return the Primary Key value for the selected record.

I truly am a newbie at this so I don't quite understand what you mean by "Usally you just do you database method and use ci input to grab the form data.", could you elaborate or is there an example you could refer me to?

Thank you.
#4

[eluser]xerobytez[/eluser]
If you have already done this in plain PHP it shouldn't be too different in CI. the flow and general idea are the same.

The View File "form.php"
Code:
<form method="post">
    <select name="dropdown">
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
        <option value="4">Option 4</option>
        <option value="5">Option 5</option>
    </select>

    <button type="submit">Find Item</button>
&lt;/form&gt;

&lt;?php if (isset($item)): ?&gt;
    &lt;?php echo $item['item_id']; ?&gt;
    &lt;?php echo $item['item_name']; ?&gt;
    &lt;?php echo $item['item_type']; ?&gt;
&lt;?php endif; ?&gt;

The Controller "form.php"
Code:
&lt;?php
    class Form extends CI_Controller {
        function index() {
            //Make sure the request is a POST
            if ($this->input->server('REQUEST_METHOD') == 'POST') {
                //Load the model to interact with the database
                $this->load->model('item_model');

                //Read the value from the form dropdown
                $item_id = $this->input->post('dropdown');

                //Fetch the item from the database by its id
                $item = $this->item_model->get_single($item_id);
                
                //If the item was found load it into the view
                if ($item) {
                    $this->load->vars('item', $item);
                }
            }
            
            //Load and display the view file
            $this->load->view('form');
        }
    }

The Model "item_model.php"
Code:
&lt;?php
    class Item_Model extends CI_Model {
        function get_single($item_id) {
            $result = $this->db->where('item_id', $item_id)->limit(1)->get('table_items');
            
            //If we found a matching item return it in array form, otherwise return false
            return ($result->num_rows()) ? $result->row_array() : false;
        }
    }

This is just to give you an idea of how the entire process should work.
#5

[eluser]InsiteFX[/eluser]
This is wrong! You are just asking for xss
Code:
//Read the value from the form dropdown
$item_id = $this->input->post('dropdown');

// Should be:
//Read the value from the form dropdown
$item_id = $this->input->post('dropdown', TRUE);  // xss_clean the input data!
#6

[eluser]QuestionBoy[/eluser]
Thank you both for the help, I will test it out shortly, but at least now I see the logic.

As for the XSS, can one not set an option in the config so you don't have to specify it in each post() or is this a best practice to always explicitly specify it?
#7

[eluser]QuestionBoy[/eluser]
Just as I think I get it, I get lost.

I could do this in a couple minutes in normal PHP, but am having a real hard time switching my brain over to CMV. I have a feeling once I get it functional it will become ridiculously simple to understand (as is most coding), but right now, it eludes me completly for some reason (I normally don't have problems picking up programming - famous last words).

I will revisit this on a rested head.
#8

[eluser]InsiteFX[/eluser]
You can load it in the config but it will always be loaded in memory, this way it is only loaded when you call it.

CodeIgniter User Guide - Form Validation

CodeIgniter User Guide - Form Helper

CodeIgniter User Guide - Input Class

CodeIgniter User Guide - Security Class




Theme © iAndrew 2016 - Forum software by © MyBB