Page 1 of 1

form validation with arrays

Posted: Mon Aug 11, 2003 10:32 am
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;
	}  
?>

Posted: Mon Aug 11, 2003 7:55 pm
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>

Posted: Tue Aug 12, 2003 12:19 pm
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;
	  }
	 }
?>