...

View Full Version : POST data validation help



seco
12-05-2010, 10:59 PM
Hi everyone, im not sure where to look or what to look for on this one.

I want to make sure all fields are filled and if not then they are ignored or alerted, would this be better with js?

example, if the last of each textbox is empty it would alert me or ignore instead of just passing an empty post data.



<input type="text" id="C0" name="C_0[]" value="entered 1" />
<input type="text" id="C0" name="C_0[]" value="entered 1" />
<input type="text" id="C0" name="C_0[]" value="" />

<input type="text" id="C1" name="C_1[]" value="entered 2" />
<input type="text" id="C1" name="C_1[]" value="entered 2" />
<input type="text" id="C1" name="C_1[]" value="" />

<input type="text" id="C2" name="C_2[]" value="entered 3" />
<input type="text" id="C2" name="C_2[]" value="entered 3" />
<input type="text" id="C2" name="C_2[]" value="" />


any help or direction would be appreciated.

tropane
12-05-2010, 11:42 PM
u can use js, but then u also will have to check if js is not disabled on user pc. Also i would recomend u to use php validation. :)

seco
12-05-2010, 11:57 PM
well i know i can use js, im asking how to validate a form with names like that.....

Lamped
12-06-2010, 12:15 AM
In simple terms, each input box would be referenced like so:



$_POST['C_0'][0]
$_POST['C_0'][1]
$_POST['C_0'][2]


$_POST['C_1'][0]
$_POST['C_1'][1]
$_POST['C_1'][2]

$_POST['C_2'][0]
$_POST['C_2'][1]
$_POST['C_2'][2]


And...



if (!@$_POST['C_1'][0]) {
echo('C_1 index 0 was empty');
}

Beeeecause... An empty or unset item converts to false in PHP. The @ silences any notices about "x is not set"... This catches both empty ("") and unset variables.

seco
12-06-2010, 12:34 AM
ok that helps, so would i have to make a


if (!@$_POST['C_1'][0]) {
echo('C_1 index 0 was empty');
}

for each text field then?

Lamped
12-06-2010, 12:41 AM
Depends how clever you want to be...



for($c=0; $c<3; $c++) {
for ($i=0; $i<3; $i++) {
if (!@$_POST['C_'.$c][$i]) {
echo('C_'.$c.' index '.$i.' was empty');
}
}
}

On the cunning scale, that code is slightly under "Fox".

seco
12-06-2010, 01:55 AM
I guess the easiest thing to do is show an array from post data, the text field amount will be different every time, could be 3 per array or it could be 100. I want to check if anything doesnt have a value and return a error.



Array
(
[title] => test
[C_0] => Array
(
[0] => 111
[1] => 111
[2] => 111
[3] =>
[4] => 111
[5] => 111
[6] => 111
)

[C_1] => Array
(
[0] => 222
[1] => 222
[2] => 222
[3] => 222
[4] => 222
[5] =>
[6] => 222
)

[C_2] => Array
(
[0] => 333
[1] => 333
[2] => 333
[3] => 333
[4] => 333
[5] => 333
[6] =>
)

)

Lamped
12-06-2010, 10:47 AM
A variable number of array items will want some research. I haven't tested this, but I expect if one is empty, it'll renumber the array and not do what you want. Perhaps naming them <input name="C_0[0]"> etc would be better. PHP *should* take the index from that, but I've never tried.

This also makes the code rather difficult. Without putting too much thought into it, you might need to run a foreach on the arrays and find the highest index number, then use that in the for loop. Like:


$current_max = 0;
foreach($_POST['C_0'] as $key => $value) {
$current_max = max($key, $current_max);
}

for($c=0; $c<$current_max; $c++)...


Now do the same sort of thing for $i, finding the highest index.

In short, you'll have to do a little research on how the browser and PHP handles an empty array entry in the middle, or end. Make the last entry empty and var_dump() it - see if the last entry actually appears in the array. If it doesn't, you'll have to set <input type="hidden" name="C_0_max" value="10" /> for C_0 having 10 entries in the HTML, and use that as $current_max...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum