473,394 Members | 1,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Input error checking on arrays..

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
8 1848
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Quijote | last post by:
Hello with this script I need to show the user (utenti) list and after I need to input in the table RICHIESTA the field COGNOME e NOME. This is the code but don't go. Someone can tell me where is...
9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
7
by: JR | last post by:
Hey all, I have read part seven of the FAQ and searched for an answer but can not seem to find one. I am trying to do the all too common verify the data type with CIN. The code from the FAQ...
6
by: Peter Krikelis | last post by:
Hi All, I am having a problem setting up input mode for serial communications. (Sorry about the long code post). The following code is what I use to set up my comm port.
2
by: headware | last post by:
I realize that when making a web application, performing input validation in the browser is good because it prevents postbacks. However, input checking that goes beyond making sure a value exists...
48
by: Michel Rouzic | last post by:
I know it must sound like a newbie question, but I never really had to bother with that before, and I didn't even find an answer in the c.l.c FAQ I'd like to know what's the really proper way...
2
by: MadMike42 | last post by:
This is really starting to annoy me, I've got a form, that has some input boxes, a example of the code is here:- <form action="admin_save_stock.asp" method="post" name="MyFormData"> <input...
20
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
27
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.