On Wed, 12 Jun 2002, Tom Ray wrote:
> I have an array question. I want to use a dynamic array but I don't know 
> how to do this. What I want to do is this, I want to write a form script 
> that takes the elements of the form and submits them in the array then I 
> want to do a loop and check to see if each element in the array has a 
> value and then if it does bullocks for them, but if it doesn't then I 
> want it to come to a screeching halt and tell them what dolts they are 
> for missing a field. So basically I want to have a Required field option 
> for the form. I'm still learning arrays and dynamic arrays are just 
> throwing me and I can't find a good code example to work from.

Here's an example of what I think you're after:


  $fields = array(
    'name' => 'Name: <input name="name">',
    'age' => 'Age: <input name="age">',
    'eyes' => 'Eyes: <input type="radio" name="eyes" value="blue"> Blue
      <input type="radio" name="eyes" value="brown"> Red'

  if (isset($_REQUEST['form_posted']))
    $missing_field = 0;
    $good_fields = '';
    foreach ($fields as $name => $html)
      if (!($val = $_REQUEST[$name]))
        if (!$missing_field)
          echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>";
        echo "<P>The following field is mandatory:<br>$html</p>";
        $missing_field = 1;
        $good_fields .= "<input type='hidden' name='$name' value='$val'>";
    if ($missing_field)
      echo $good_fields
        . '<input type="submit" name="form_posted"></form>';

      echo "<p>Thank you for your input! Here's what we got:</p><ul>";
      foreach (array_keys($fields) as $name)
        echo "<li>$name: {$_REQUEST[$name]}</li>";
      echo '</ul>';
    echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>";
    foreach ($fields as $f)
      echo "$f<br>";
    echo '<input type="submit" name="form_posted"></form>';


PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to