By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,730 Members | 1,399 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,730 IT Pros & Developers. It's quick & easy.

Input error checking on arrays..

P: n/a
If I want to check for input of an integer I've got the following (I
get the form input with $input = "$_POST[input]"):

if(!ereg("^[0-9]+$",$_POST[input])) {
echo "Input is incomplete or incorrect.";
}

If, instead of only getting one 'input' I wanted to get n instances of
input, I'd generate input fields for each of n instances I want in a
for loop, then get the input with:

$input[$cnt] = $_POST["input"][$cnt];

Of course, then if I've got the following:

if(!ereg("^[0-9]+$",$_POST["input"][$cnt])) {
echo "Input is incomplete or incorrect.";
}

I'll get the error output if or if not the input was correct or as
intended (in this case integer(s) only). It's clearly an issue with
either the formatting of the ereg condition, or with getting the input
data itself (I'm not certain which). I've had a few ideas and changed
a few things, included a few others, etc.; but I've gotten nowhere
with it on my own yet and instead of wasting others' time with giving
those incorrect examples on here I'd be open to (and grateful for)
insights on what I ought to do in order to get this to work.

Thank you,

- Oeln
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In article <ff**************************@posting.google.com >,
oh*****@yahoo.com (Oeln) wrote:
$input[$cnt] = $_POST["input"][$cnt];

Of course, then if I've got the following:

if(!ereg("^[0-9]+$",$_POST["input"][$cnt])) {
echo "Input is incomplete or incorrect.";
}


It's unclear what exactly $cnt is supposed to be or where it gets its
value from. Try do a:

echo '<pre>';
print_r($_POST);
echo '</pre>';

To see what $_POST looks like after submitting. Is $_POST['input']
actually an array? What is the value of $cnt? Does $_POST['input'] have
a key that is equal to the value of $cnt?

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #2

P: n/a
Jan Pieter Kunst <de*****@cauce.org> wrote in message news:<de***************************@news1.news.xs4 all.nl>...
In article <ff**************************@posting.google.com >,
oh*****@yahoo.com (Oeln) wrote:
$input[$cnt] = $_POST["input"][$cnt];

Of course, then if I've got the following:

if(!ereg("^[0-9]+$",$_POST["input"][$cnt])) {
echo "Input is incomplete or incorrect.";
}


It's unclear what exactly $cnt is supposed to be or where it gets its
value from. Try do a:

echo '<pre>';
print_r($_POST);
echo '</pre>';

To see what $_POST looks like after submitting. Is $_POST['input']
actually an array? What is the value of $cnt? Does $_POST['input'] have
a key that is equal to the value of $cnt?

JP


Yes, initially I would get input indicating the number of 'inputs' one
wants to be offered ($input_cnt, for instance). On the next form, I
get

$input_cnt = "$_POST[input_cnt]";

In a for loop, I offer $input_cnt number of 'inputs':

for($cnt=0; $cnt<$input_cnt; $cnt++) {
echo "<input type=\"text\" name=\"input\">";
}

I include <input type"hidden" name="input_cnt" value="$input_cnt"> in
this form, too.

On the next (i.e., third) form, I want to get the data for each of the
inputs:

for($cnt=0; $cnt<$input_cnt; $cnt++) {
$input[$cnt] = $_POST["input"][$cnt];
}

I want to check the input for each with ereg (in this case I only want
integers, for example)..

- Oeln
Jul 17 '05 #3

P: n/a
On Fri, 09 Apr 2004 11:42:20 -0700, Oeln wrote:
Yes, initially I would get input indicating the number of 'inputs' one
wants to be offered ($input_cnt, for instance). On the next form, I
get

$input_cnt = "$_POST[input_cnt]"; ^ ^

Better written (IMO):

$input_cnt = $_POST['input_cnt'];

In a for loop, I offer $input_cnt number of 'inputs':

for($cnt=0; $cnt<$input_cnt; $cnt++) {
echo "<input type=\"text\" name=\"input\">";
}

And you've just severely broken multiple fields. They'd all be called
'input' so if you $cnt == 10.. only the first value will be used.

Single quotes would be netter here also:
echo '<input type="text" name="input">';

(but 'input' never increments for submission purposes).


I include <input type"hidden" name="input_cnt" value="$input_cnt"> in
this form, too.

On the next (i.e., third) form, I want to get the data for each of the
inputs:

for($cnt=0; $cnt<$input_cnt; $cnt++) {
$input[$cnt] = $_POST["input"][$cnt];
}

I want to check the input for each with ereg (in this case I only want
integers, for example)..

is_int() should perform this part for you.

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #4

P: n/a
In article <ff**************************@posting.google.com >,
oh*****@yahoo.com (Oeln) wrote:
In a for loop, I offer $input_cnt number of 'inputs':

for($cnt=0; $cnt<$input_cnt; $cnt++) {
echo "<input type=\"text\" name=\"input\">";
}

I include <input type"hidden" name="input_cnt" value="$input_cnt"> in
this form, too.


I see. The problem here is that you don't create an array of "input".
The last (or first, not sure) of the fields will 'win' and you end up
with just one value in $_POST, not an array.

You could do it like this:

for($cnt=0; $cnt<$input_cnt; $cnt++) {
echo "<input type=\"text\" name=\"input[]\">";
}

Note [] after the "name": that gives you a numeric array called "input".
Also, it's not necessary to put the "count" of that array in an hidden
variable. Arrays know their size.

Upon receiving the submitted page, you can now do:

foreach($_POST['input'] as $input) {
check_for_desired_characteristics($input);
}

HTH,
JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #5

P: n/a
Jan Pieter Kunst <de*****@cauce.org> wrote in message news:<de***************************@news1.news.xs4 all.nl>...
In article <ff**************************@posting.google.com >,
oh*****@yahoo.com (Oeln) wrote:
In a for loop, I offer $input_cnt number of 'inputs':

for($cnt=0; $cnt<$input_cnt; $cnt++) {
echo "<input type=\"text\" name=\"input\">";
}

I include <input type"hidden" name="input_cnt" value="$input_cnt"> in
this form, too.


I see. The problem here is that you don't create an array of "input".
The last (or first, not sure) of the fields will 'win' and you end up
with just one value in $_POST, not an array.

You could do it like this:

for($cnt=0; $cnt<$input_cnt; $cnt++) {
echo "<input type=\"text\" name=\"input[]\">";
}

Note [] after the "name": that gives you a numeric array called "input".
Also, it's not necessary to put the "count" of that array in an hidden
variable. Arrays know their size.

Upon receiving the submitted page, you can now do:

foreach($_POST['input'] as $input) {
check_for_desired_characteristics($input);
}

HTH,
JP


Oh, I'm an idiot - I've got the '[]'. I forgot to include that; but
it's in there. I've got no issue getting the data itself, it's only
checking the input with the ereg condition in this case which I've got
an issue with.. ;/
Jul 17 '05 #6

P: n/a
In order to illustrate the idea I've got, I'll offer the following:

if(
foreach($_POST[input]) {
(!ereg("^[0-9]+$",$_POST[input]))
}) {
include('input_errchk.inc');
} else { ..

Inside an if loop, I want to figure out if the input is *not* as
intended (i.e., as indicated in the ereg condition). If it, or if one
of them, is not, I want to include a certain file which outputs an
error indicating this fact; otherwise, continue onward..

I'm not certain if I can "if(foreach(.." etc. - I get the impression
it oughtta work; but I'm only getting an error with it. (I've of
course changed this a few times to include certain things in case I'm
only a little bit off - including $_POST['input'] or $_POST["input"]
instead of $_POST[input], $_POST[input] as $input, etc.)

It would be ideal if instead of even going with a foreach loop, I
could compare the array itself to an ereg condition instead of each of
the individual 'inputs' in the array (I'm only not certain what to
include in this, or if it's an option). I'd want to indicate the array
could include integers only, and whatever is in between them in the
array in order to isolate one from the other. It's only got to include
one 'input', but it could include 10, for instance. I'd imagine this
would be like "^[0-9]+" for at least one, then "[0-9]*$" for the
others, but I'm not certain what else I'd include in the ereg
condition itself..

- Oeln
Jul 17 '05 #7

P: n/a
In article <ff************************@posting.google.com>,
oh*****@yahoo.com (Oeln) wrote:
I'm not certain if I can "if(foreach(.." etc. - I get the impression
it oughtta work; but I'm only getting an error with it. (I've of
course changed this a few times to include certain things in case I'm
only a little bit off - including $_POST['input'] or $_POST["input"]
instead of $_POST[input], $_POST[input] as $input, etc.)

It would be ideal if instead of even going with a foreach loop, I
could compare the array itself to an ereg condition instead of each of
the individual 'inputs' in the array (I'm only not certain what to
include in this, or if it's an option).


No, that's an impossible construct you are trying to make. If you want
to check every member of the array for certain characteristics, the
"foreach" has to be outermost and the check with its "if" has to be
inside that foreach loop.

And you can't check every member of an array without looping through the
array.

See here for basic info:

<http://www.php.net/manual/en/control-structures.php>

JP

--
Sorry, <de*****@cauce.org> is een "spam trap".
E-mail adres is <jpk"at"akamail.com>, waarbij "at" = @.
Jul 17 '05 #8

P: n/a
Jan Pieter Kunst <de*****@cauce.org> wrote in message news:<de***************************@news1.news.xs4 all.nl>...
In article <ff************************@posting.google.com>,
oh*****@yahoo.com (Oeln) wrote:
I'm not certain if I can "if(foreach(.." etc. - I get the impression
it oughtta work; but I'm only getting an error with it. (I've of
course changed this a few times to include certain things in case I'm
only a little bit off - including $_POST['input'] or $_POST["input"]
instead of $_POST[input], $_POST[input] as $input, etc.)

It would be ideal if instead of even going with a foreach loop, I
could compare the array itself to an ereg condition instead of each of
the individual 'inputs' in the array (I'm only not certain what to
include in this, or if it's an option).


No, that's an impossible construct you are trying to make. If you want
to check every member of the array for certain characteristics, the
"foreach" has to be outermost and the check with its "if" has to be
inside that foreach loop.

And you can't check every member of an array without looping through the
array.

See here for basic info:

<http://www.php.net/manual/en/control-structures.php>

JP


Okay, well I've figured out a way to get it to work. I initially get
the input data in a for loop, then compare with the intended ereg
condition. If it fails once, $input_errchk = 1

I've then got an if loop where if($input_errchk) {
include('input_errchk.inc'); die; ..in other words, I include the
input error output file I've got (in which I indicate what the error
is) instead of whatever else I would output, etc. etc.

Thanks for the input on this, Jan.

- Oeln
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.