form validation with arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

form validation with arrays

Post by CrazyJimmy »

I have a form which submits a array and I need to check if its empty or not. I'm trying to use the following code snippet but it does'nt work. discs_size is the array.

Code: Select all

<?php

IF (empty($_POST['discs_size'])) 
    { 
      echo '<H3>Error: Click <a href="addtitle.php?action=1">here</a> to try again</H3>';
	  break;
	}  
?>
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Not sure this is the right approach, but it works.

Code: Select all

<?php
    if (empty($_POST['discs_sizes'])) { 
        echo 'Empty'; 
    }
    else if (is_array($_POST['discs_sizes'])) { 
        echo $_POST['discs_sizes'][0]; 
    } 
?> 
<form method="post">
    <input type="hidden" name="discs_sizes[]" value="foo">
    <input type="submit">
</form>
CrazyJimmy
Forum Commoner
Posts: 34
Joined: Tue Nov 19, 2002 1:40 pm

Post by CrazyJimmy »

I managed to get it to work using the following code, im not that familiar with foreach statement and am trying to modify for 2 dimensional arrays.

Code: Select all

<?php
foreach($_POST['discs_size'] as $val) 
     {
   	if(empty($val) || $val == "" || !is_numeric($val)) 
	 {
	  echo '<H3>Error: Please Fill In All Values</H3>'; 
	  break 2;
	  }
	 }
?>
Post Reply