473,545 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1850
In article <ff************ **************@ posting.google. com>,
oh*****@yahoo.c om (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.new s.xs4all.nl>...
In article <ff************ **************@ posting.google. com>,
oh*****@yahoo.c om (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_c nt"> 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_c nt"> 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.c om (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_c nt"> 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_desir ed_characterist ics($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.new s.xs4all.nl>...
In article <ff************ **************@ posting.google. com>,
oh*****@yahoo.c om (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_c nt"> 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_desir ed_characterist ics($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************ ************@po sting.google.co m>,
oh*****@yahoo.c om (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.new s.xs4all.nl>...
In article <ff************ ************@po sting.google.co m>,
oh*****@yahoo.c om (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_errch k) {
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
1713
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 the mistake??? Many Thanks!! <select name="user" id="user"> <? $q= mysql_query("select * from utenti order by user");
9
3197
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 "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
7
7274
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 looks like this: #include <iostream>
6
6962
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
1248
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 or that it's not alphabetic when it's supposed to be numeric is arguably best left to the business tier code on the server where there is access to...
48
2204
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 for input a string in an array of char that's dynamically allocated. I mean, I wish not to see any such things as char mystring; I don't want to see...
2
1481
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 name="Make" type="text" id="MenuText" value=<% response.write rsTable("Make") %size="50" maxlength="50"> <input name="DateBought" id="MenuText"...
20
5104
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 to do it word by word. I am confused as to how to do this. Typically, I would think it would make sense to try and input the words into strings, but...
27
3107
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 that's causing the problem. Here's how I wrote the program originally: #include <stdio.h> #include <string.h>
0
7393
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5965
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5322
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4942
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1871
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
695
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.