Jump to content

Best use of php filter ( array or input)


scotchegg78

Recommended Posts

HI guys

 

 

I have a form collecting just ..

 

15 input boxes of numeric numbers 1-10

a colour enum- red, white yellow or blue.

hidden userid field.

 

is it better to have 15 instances of..

$score_1 = filter_input(INPUT_POST, 'score_1', FILTER_VALIDATE_INT); 
$score_2 = filter_input(INPUT_POST, 'score_2', FILTER_VALIDATE_INT); 
$score_3 = filter_input(INPUT_POST, 'score_3', FILTER_VALIDATE_INT);

 

or do something with the php input array function?

 

Does it really make a performance difference?

 

thanks for advice.

 

 

Link to comment
https://forums.phpfreaks.com/topic/173146-best-use-of-php-filter-array-or-input/
Share on other sites

ok thanks

 

this maynot make sense, but then would i not have to put my form 15 inputs into an array?

 

like

 

<input type="text" class="text" name="score[]" id="score_1" />
<input type="text" class="text" name="score[]" id="score_2" />
<input type="text" class="text" name="score[]" id="score_3" />

etc

 

sorry just not used php filter array before.

 

and to keep the entered input on page reload how do i access these from the post array?

is it..

<input type="text" class="text" name="score[]" id="score_1"  [b]value="<?php echo $_POST['score[1]']?>"[/b]/>

??

that looks wrong!

First of all, remember that enumerated arrays are zero indexed! Secondly, $_POST is an array, as you know, so if you need to access an array inside $_POST you do like $_POST['some_array']['some_index'].

 

As for the form, you can do like you did, but you can also explicitly specify the index like this:

<input type="text" class="text" name="score[5]" id="score_5" />

 

You'll need to set a value for the checkboxes though. To make it checked when the page loads you will have to add checked="checked".

hi dan

 

i am not using checkboxes, its 15 input boxes for int values only.

 

so my load values from the post on page load into form would be..

 

<input type="text" class="text" name="score[5]" id="score_5" value="<?php echo $_POST['score'][5]"?>" />

 

? Is that right?

 

thanks

 

i am not using checkboxes, its 15 input boxes for int values only.

 

Ah, sorry. I could have sworn I saw the word "checkbox" somewhere.

 

so my load values from the post on page load into form would be..

 

<input type="text" class="text" name="score[5]" id="score_5" value="<?php echo $_POST['score'][5]"?>" />

 

? Is that right?

 

Looks so. You could try it out and see if it works ;)

 

You'll need to use isset to make sure you aren't accessing an invalid index though.

thanks i will, just trying to get my head around the use of array filter on my 15 input boxes!

 

is this it for just the score post array then..

 

$filter = array('score' =>  FILTER_SANITIZE_INT);
$cleaninputs = filter_input_array( INPUT_POST, $filter );

 

or if i had another post in their like colour for example its..

 

$filter = array(
'score' =>  FILTER_SANITIZE_INT,
'colour' =>  FILTER_SANITIZE_STRING);

$cleaninputs = filter_input_array( INPUT_POST, $filter );

 

thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.