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
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!

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.