473,395 Members | 1,692 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,395 software developers and data experts.

Array Manipulation , allowing only numeric entries in Text field

123 100+
Hi all,
I have one webpage in which people can order books online.
i have one array - book , in which i have stored all different types of books.
Now, I dont want the user to order any garbage ( just numerical entry and not alphabetics ) .
How can I do so ??
I have used following code , but dint work proper
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['books']) ) {
  2. $input = $_POST['books'];
  3. foreach($input as $input1)
  4. {
  5.     if(!(ereg("^[0-9]+$",$input1))) die ('Please enter numeric values ') ;
  6. }
  7. }
  8.  
in Above , I want to check for all the entries of books , whether the value entered is numeric or not....
But I guess I am not populating array properly , So how can I do so ??
Jul 9 '08 #1
15 3282
hsriat
1,654 Expert 1GB
Check your $_POST variable.[php]echo "<pre>";
print_r($_POST);
echo "</pre>";[/php]

And use is_int.[php]if (!is_int($input1)) die ('error.... .. ');[/php]
Jul 9 '08 #2
ajd335
123 100+
Check your $_POST variable.[php]echo "<pre>";
print_r($_POST);
echo "</pre>";[/php]

And use is_int.[php]if (!is_int($input1)) die ('error.... .. ');[/php]
Hey Hsriat,
When i code echo "<pre>" ........echo "</pre>"
i got following O/P
Expand|Select|Wrap|Line Numbers
  1. <pre>Array
  2. (
  3.     [customername] => 
  4.     [email] => 
  5.     [books] => Array
  6.         (
  7.             [book1] => 22
  8.             [book2] => 
  9.         )
  10.  
  11.     [enroll] => Array
  12.         (
  13.             [s1] => 
  14.             [s2] => 
  15.         )
  16.     [comments] => 
  17.  
but , e'time it goes to die portion only even if i have provided numeric values...( also in the code that you have given )
Thanks for your help Hsriat
Jul 9 '08 #3
hsriat
1,654 Expert 1GB
I see that $_POST[books][book2] is empty. Perhaps, this is causing the problem.
There might be something wrong with your form HTML.

Post the HTML part of the form being used.
Jul 9 '08 #4
ajd335
123 100+
I see that $_POST[books][book2] is empty. Perhaps, this is causing the problem.
There might be something wrong with your form HTML.

Post the HTML part of the form being used.
Hi Hsriat ,
book2 field is empty as I have not provided ( i have not ordered that book)..I just want to order book1 .....So the numeric value is number of orders made...And i have to check that numbers only whether they are numerical or not , as no one can write some alphabetics in that field..

Thanks,
Jul 9 '08 #5
hsriat
1,654 Expert 1GB
Ok, for that, we can add some code to the PHP side, but I would suggest you to make a change in the form HTML, so that only that book id is posted which is selected, not the unselected ones.

How are you selecting the books?. Checkbox or what?
Jul 9 '08 #6
ajd335
123 100+
Ok, for that, we can add some code to the PHP side, but I would suggest you to make a change in the form HTML, so that only that book id is posted which is selected, not the unselected ones.

How are you selecting the books?. Checkbox or what?
Hi hsriat,
Expand|Select|Wrap|Line Numbers
  1. Small book, Volume I</td>
  2. <td align="center">$70x <input type="text" name="books[book1]"
  3. size="2" /></td>
  4.  
user can enter the quantity of book1 in books[book1] text box..or they can leave it empty if they dont wnat book1 and want book2 ...book3....and so on
hope I made it clear.
and ya , the only book selected are shown in the final order-made page.
the array i just show you to have an Idea.
Thnaks
Jul 9 '08 #7
hsriat
1,654 Expert 1GB
Remove book1 etc from withing the square braces. Let it be books[] in each input, and try again.
Jul 9 '08 #8
ajd335
123 100+
Hey hsriat,
I think the way I am approaching the array book is not valid ,
As
Expand|Select|Wrap|Line Numbers
  1.  foreach($_POST['books']  as $input1)
  2. {
  3. echo $input1 ;
  4. /* if(!(ereg("^[0-9]+$",$input1)))
  5. {
  6. //  die ('Please enter numeric values in the Quantity fields') ;
  7. } */
  8. }
  9.  
i have just commented out the if statement n just checked the values of input1 , its just an variable seems(not array) as i have selected two books 11 ,22 quntity each,
input1 shows me 1122 as the value..Is it causing some problem,
because when i used above code for all the single entry i.e

Expand|Select|Wrap|Line Numbers
  1.  if(isset($_POST['books']['book1']) ) { $input = $_POST['books']['book1'];
  2. if(!(ereg("^[0-9]+$",$input)))
  3. { die ('Please enter numeric values in the Quantity fields') ; }}
  4.  
it gives me perfect desire answer..
So may be some problem with array manipulation ...what you suggest??
Jul 9 '08 #9
ajd335
123 100+
Hi Hsriat,
Removing the book1 and ... options may not be proper ,as we have huge number of coding after that..so is there any other way ?
Jul 9 '08 #10
hsriat
1,654 Expert 1GB
[php]$selected_books = array();
foreach ($_POST['books'] as $input)
{
if (is_int($input))
array_push($input, $selected_books);
elseif ($input != "")
die ('...error...');
}
//$selected_books contains the array of your required books.[/php]

Try this...
Jul 9 '08 #11
ajd335
123 100+
[php]$selected_books = array();
foreach ($_POST['books'] as $input)
{
if (is_int($input))
array_push($input, $selected_books);
elseif ($input != "")
die ('...error...');
}
//$selected_books contains the array of your required books.[/php]

Try this...
Hi hsriat,
Thanks for the help...But i want to discard the whole order if one has entered any alphabetics in that text box
The above code does not give me any answer.
Thanks,
Jul 9 '08 #12
ajd335
123 100+
hey Hsriate,
The problem is solved now..
Thanks ..
Jul 9 '08 #13
hsriat
1,654 Expert 1GB
But i want to discard the whole order if one has entered any alphabetics in that text box
That's what it is doing!

Didn't you try it?
Jul 9 '08 #14
hsriat
1,654 Expert 1GB
hey Hsriat,
The problem is solved now..
Thanks ..
You are welcome.
Jul 9 '08 #15
ajd335
123 100+
That's what it is doing!

Didn't you try it?
Yeah , I do try that, but IT dint give me any answer..
But i have taken two dimensional array and compared it.n it worked !!
Jul 9 '08 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Michelle | last post by:
hi, i have created an array from recordset containing user names eg. (davidp, davidp, evenf, patricka, rebeccah) which i have sorted in alphabetical order, but i need to identify duplicates...
3
by: John MacIntyre | last post by:
Hi, Can anybody give me a hint as to how to convert a javascript array into a vbscript array? BTW-it only needs to work in IE5 & 6 Thanks in advance, John MacIntyre VC++ / VB / ASP /...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
4
by: Bob P. | last post by:
Hello, I have a page with: * two side-by-side asp:listboxes and two arrow asp:buttons allowing users to add/remove email addresses between them -- very much like Outlook, where you have the...
12
by: Sheldon | last post by:
Hi, I have two arrays that are of the same dimension but having 3 different values: 255, 1 or 2. I would like to set all the positions in both arrays having 255 to be equal, i.e., where one...
5
by: Reny | last post by:
can any one tell how can i restrict my user to type just numeric character in the textbox.I am using VS.NET 2003 (VB.NET)
1
by: lukemack | last post by:
Hi, I have an array in the following format : Array ( =Array ( =Array (
3
by: OliveOyl3471 | last post by:
In Visual Basic.NET 2003, how do you populate a two dimensional array from a text file? I know how to check file existence, open the file for read, etc. I just can't get it to read more than one...
13
by: Daniel Klein | last post by:
I have a class constructor that accepts an array as the only argument. The catch is that the array MUST be an 'integer-indexed' array, not an 'associative' array, because the index position has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.