Welcome Guest, Not a member yet? Register   Sign In
Code Improvements & Validation
#1

[eluser]blue.sky[/eluser]
I have two questions:
1. Improving my code, especially allowing user selections in any order. At the moment even if values were correctly matched the order of selection matters.

2. Please include any necessary input validations. Dropdown list values are from the db.

Code:
public function select() {
      $this->load->view('select_view'); // view below
  }

  public function select_validate() {
      if($this->input->post(NULL,TRUE)) {
      $group1 = $this->input->post('group_1');
      $group2 = $this->input->post('group_2');
      $group3 = $this->input->post('group_3');
      $match1 = $this->input->post('match_1');
      $match2 = $this->input->post('match_2');
      $match3 = $this->input->post('match_3');
}
      if(($group1 == 'fruit' & $match1 == 'pineapple')&
         ($group2 == 'reptile' & $match2 == 'cobra')&
         ($group3 == 'panthera' & $match3 == 'leopard')) {
        echo "Correct<br />";
      } else {
        echo "Wrong<br />";
      }  
  }
Code:
// select_view.php
&lt;html&gt;
&lt;body&gt;
&lt;?php $groups = array('fruit'=>'Fruits', 'reptile'=>'Reptile', 'panthera'=>'Panthera');?&gt;
&lt;?php $match = array('Make a Pick','leopard'=>'Leopard','pineapple'=>'Pineapple','cobra'=>'Cobra');?&gt;
    
Select Values coming from db
    &lt;?php echo form_open('/web/select_validate'); ?&gt;
     <table> //Visuals @ http://jsfiddle.net/cyCRt/1/
        <tr>
            <td>&lt;?php echo form_dropdown('group_1',$groups);?&gt;</td>
            <td>&lt;?php echo form_dropdown('match_1',$match);?&gt;</td>
        </tr>
        <tr>
            <td>&lt;?php echo form_dropdown('group_2',$groups);?&gt;</td>
            <td>&lt;?php echo form_dropdown('match_2',$match);?&gt;</td>
        </tr>
        <tr>
            <td>&lt;?php echo form_dropdown('group_3',$groups);?&gt;</td>
            <td>&lt;?php echo form_dropdown('match_3',$match);?&gt;</td>
        </tr>
    </table>
    &lt;?php echo form_submit('submit','Enter Selection');?&gt;
    &lt;?php echo form_close(); ?&gt;
&lt;/body&gt;
&lt;/html&gt;
All answers are welcomed. The many improvements the more I learn. Thank You
#2

[eluser]InsiteFX[/eluser]
I would add TRUE to your inputs for XSS, unless you are using xss_clean in your form validation rules.
Code:
$group1 = $this->input->post('group_1', TRUE);
#3

[eluser]blue.sky[/eluser]
[quote author="InsiteFX" date="1339998929"]I would add TRUE to your inputs for XSS, unless you are using xss_clean in your form validation rules.
Code:
$group1 = $this->input->post('group_1', TRUE);
[/quote]
Noted. Thanks
#4

[eluser]skunkbad[/eluser]
It seems that you are doing things correctly, but I am having a hard time understanding why you would do what you are doing. Is this for a quiz where matching is graded? Maybe if we know what you are doing, we can make suggestions...
#5

[eluser]blue.sky[/eluser]
[quote author="skunkbad" date="1339999670"]It seems that you are doing things correctly, but I am having a hard time understanding why you would do what you are doing. Is this for a quiz where matching is graded? Maybe if we know what you are doing, we can make suggestions...[/quote]

Yes it's for a Quiz. Thanks




Theme © iAndrew 2016 - Forum software by © MyBB