Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validation to an array of textboxes

Status
Not open for further replies.

misslois74

Programmer
Sep 27, 2008
63
PH
im using an array of textboxes and put a validation on those textboxes in which if a user puts in a non-numeric value it should display an error message and would not proceed but it seems that my validation is not working bec. eventhough i've already entered a numeric value im still getting the error message and would not proceed on the next step...

here is the validation:
Code:
function validate_input()
								{
									global $errors;
									if(!isset($_POST["qty"]) || trim($_POST["qty"]) == "")
									{
										$errors['qty1'] = "<font color='red' size='1'>*</font>";
										$errors['qty2'] = "<font color='red' size='1'> * Choose at least one item</font><br>";
									}
									else if(!is_numeric(trim($_POST["qty"])))
									{
										$errors['qty3'] = "<font color='red' size='1'>*</font>";
										$errors['qty4'] = "<font color='red' size='1'>Should be numeric value</font><br>";
									}
									else if(isFloat(trim($_POST["qty"])))
									{
										$errors['qty5'] = "<font color='red' size='1'>*</font>";
										$errors['qty6'] = "<font color='red' size='1'>Should be integer value</font><br>";
									}
								}

and here is the form:

Code:
function display_form()
								{
									global $errors;
							?>
		            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
                                  <p>Enter the quantity of the product you wish to order.</p><br />
					<?php  if(isset($errors['qty2']))
					 	   { 
						   		echo $errors['qty2'];
						   } 
						   else if(isset($errors['qty4']))
					 	   { 
						   		echo $errors['qty4'];
						   }
						   else if(isset($errors['qty6']))
					 	   { 
						   		echo $errors['qty6'];
						   }  ?>
                            
                            
							<table width="650" border="0">
                                          <tr>
                                            <td width="135">Product Code</td>
                                            <td width="153"><div align="center">Product Name</div></td>
                                            <td width="103"><div align="center">Image</div></td>
                                            <td width="67"><div align="center">Price</div></td>
                                            <td width="56"><div align="center">&nbsp;</div></td>
                                            <td width="110"><div align="center">Quantity</div></td>
                                          </tr>
                                        </table>

                  						<table width="655" border="0">
								<?php
									//print items from the catalog for selection
									include "db_config.php";
									$quer = "Select * from tbl_candle order by product_name";
									$res = mysql_query($quer) or die(mysql_error());
									//$nums 
									while($rows = mysql_fetch_array($res))
									{  ?>
										<tr><td width="176"><b><?php echo $rows[product_code]; ?></b></td>
										<td width="173"><b><?php echo $rows[product_name]; ?></b></td>
										<td width="125"><a href="<?php echo $rows[product_image]; ?>" onclick="window.open(this.href, 'child', 'height=200, width=250'); return false"><img src="<?php echo $rows[product_image]; ?>" width="74" height="69" title="CLICK TO ENLARGE"></a></td>
										<td width="70"><b><?php echo $rows[product_price]; ?></b></td>
                                        <td><center><input type="checkbox" name="order[]" value="<?php echo $rows[product_id]; ?>" onClick="myFunction('<?php echo $rows['product_id']. "qty"; ?>')"></center></td>
										<td><center><input type="text" name="qty[<?php echo $rows['product_id']; ?>]" size="5" id="<?php echo $rows['product_id']."qty"; ?>" disabled></center></td><td><?php echo $errors['qty1']; echo $errors['qty3']; echo $errors['qty5']; ?></td></tr>
										
							  <?php }
								}  ?>
								
								<tr>
								<td colspan="5"><br />
                                <input type="hidden" value="additems" name="action" />
								<center><input type="submit" name="submit" value="Add items to order" title="Click to Order"></center>
								</td>
								</tr>
								</table>
                         </form>

thanks in advance.


 
From what I can gather, your $_POST['qty'] variable is an array, yet you are performing checks on it as if it where a single value.

Basically you are checking to see whether your array of qty text boxes is numeric, but of course its not, because its an array.

You need to check each element of your array to validate its actually numeric.

Use a loop to go through each value inside the qty array and check if its numeric.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top