Member Avatar for skinbug

Hi all, I've hit a brick wall trying to validate some array data. I'll dive right in with the code...

<form action="process.php" method="post">
<ul>
<li><label>Order: <?php echo $form->error("order"); ?></label><input class="order" type="text" name="task[1][order]" maxlength="3" value="<?php echo $form->value("order"); ?>" /></li>
<li><label>Task: <?php echo $form->error("task"); ?></label><input type="text" name="task[1][task]" maxlength="50" value="<?php echo $form->value("task"); ?>" /></li>
</ul>

<ul>
<li><label>Order: <?php echo $form->error("order"); ?></label><input class="order" type="text" name="task[2][order]" maxlength="3" value="<?php echo $form->value("order"); ?>" /></li>
<li><label>Task: <?php echo $form->error("task"); ?></label><input type="text" name="task[2][task]" maxlength="50" value="<?php echo $form->value("task"); ?>" /></li>
</ul>

<input type="hidden" name="subtasks" value="1" />
<input class="submit" id="submit" type="submit" value="Submit Tasks" />

</form>

So the HTML form consists of two field 'groups', task[1][name] and task[2][name]. If the user populates both 'groups', then data inserts into two rows of a database. If they complete just one 'group', then one row is inserted.

if(isset($_POST['subtasks'])) { // User submitted project task form
   $this->procTasks();
}

// procTasks
function procTasks() {
   global $session, $form;
		
   $validation_failed = false;
   
   // New tasks attempt
   foreach ($_POST['task'] as $task => $task_field) {
      if (!empty($task_field['task'])) {
         if (false === $session->newTasks($task_field['order'],$task_field['task'])) {
            $validation_failed = true;
         }
      }
   }

   // New tasks successful
   if(!$validation_failed) {
      $_SESSION['newtasks'] = true;
      header("Location: ".$session->referrer);
   }
   else if($validation_failed == 1) { // Error found with form
      $_SESSION['value_array'] = $_POST;
      $_SESSION['error_array'] = $form->getErrorArray();
      header("Location: ".$session->referrer);
   }
   else if($validation_failed == 2) { // Registration attempt failed
      $_SESSION['newtasks'] = false;
      header("Location: ".$session->referrer);
   }
}

This is where the data is processed to be posted out. I've done a quick check to see if the task fields are empty, but values are sent to session->newTasks for validation.

// newTasks
function newTasks($suborder, $subtask) {
   global $database, $form;

   // This is where $suborder and $subtask will be validated

   // This is where I'm having real problems!

   // Typical validation would be to make sure a complete task has been created, all necessary fields populated
   // To make sure that the order can only be numbers, no more than 3 digits etc

   if($form->num_errors > 0) {
      return 1; // Errors with form
   }
   else { // No errors, add the new task
      if($database->addNewTasks($suborder, $subtask)) {
         return true; // New project added succesfully
      }
      else {
         return 2; // New task attempt failed
      }
   }
}

So at this point, the data has three options:
1. There is an error ($form->num_errors > 0) - return 1.
2. There are no errors, go to the database - return true. The database makes the connection, performs mysql_real_escape_string, then inserts the values.
3. Unkown failure - return 2.

// Form handles values and errors
var $values = array(); // Holds submitted form field values
var $errors = array(); // Holds submitted form error messages

// value - Returns the value attached to the given field.
function value($field) {
   if(array_key_exists($field,$this->values)) {
      $val = $this->values[$field];
      if(is_array($val)) {
         return $val;
      }
      else {
         return htmlspecialchars(stripslashes($val));
      }
   }
   else {
      return "";
   }
}


// error - Returns the error message attached to the given field.
function error($field) {
   if(array_key_exists($field,$this->errors)) {
      return "<p class='errormsg'>".$this->errors[$field]."</p>";
   }
   else {
      return "";
   }
}

The form.php stores the correct values and the errors in the user submitted form.

Now as it stands, with no validation taking place, I can insert one or two tasks depending on how I populate the html form, into one or two rows of the database and it works seemlessly. As soon as I try to introduce validation (checking 'is_array', 'sizeof'...) then the script fails. I have not got as far as validating the array values yet, but that would be necessary.

The areas of the script I need help with are function newTasks where validation is carried out, and the form that handles the errors and values. I'm finding it hard to understand how $suborder and $subtask are storing data - in arrays or not. But this is the point of validation in the script.

Thanks in advance for any help :)

have you tried is_object instead of is_array ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.