Checkbox form if(..'selected') error

Hi,
Is there anything wrong with the syntax for this array of checkboxes? I can’t see anything wrong and I am getting an error for the line with the ‘if’ statement.

<fieldset>
 <legend>Fields to include:</legend>
   <?php foreach($fields as $field):?>
     <div><label for="field<?php htmlout($field['id']); ?>">
     <input type="checkbox" name="fields[]" id="field<?php htmlout($field['id']); ?>" value="<?php htmlout($field['id']);?>"
      <?php
        if($field['selected'])
            {
              echo ' checked';
            }
       ?>><?php htmlout($field['type']); ?>
          </label>
     </div>
    <?php endforeach; ?>
</fieldset>

Any help would be greatly appreciated,
Thanks,
Shane

Is the KEY ‘selected’ defined for all $fields? e.g. 0 or 1, empty or 1?

If not you might need to go with isset()

if(isset($field['selected']))

yeas…

if(isset($field[‘selected’]))
{
echo “selected”;
}
else
{
echo “not selected”;
}

Hi,

No and I now think this is the problem. I am using examples in Kevin Yank’s Sitepoint book, PHP & MySQL Novice to Ninja. In the 5th edition of this book on page 226 he does use,

foreach ($result as $row)
{
$categories[] = array(
'id' => $row['id'],
'name' => $row['name'],
'selected' => FALSE);
}

Maybe this is where he sets ‘selected’. But I don’t really get this idea of ‘selected’ and ‘checked’.
Thanks,
Shane

Well it has been about six weeks since I have had a chance to work on this. But I am back now!

I have now added “select” => FALSE

to the $fields array, as can be seen in this HTML below. And there is now no error in error_log.
The issue now is that when I hit submit, the form returns to itself.

The search.html.php

<?php include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/helpers.inc.php';  
      include_once $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/func.inc.php'; 
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildcourses.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildlearners.inc.php';
      include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/buildnotes.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
  <body background="/artgibney/includes/retro_intro_@2X.png">
      <p><a href="..">physCMS home</a> &#8658; <a href="/artgibney/admin/notes/">Manage Daily Notes</a></p>
      <?php $fields = array(
        1 => array("type" => "absence", "id" => "1", "selected" => FALSE),
        2 => array("type" => "late", "id" => "2", "selected" => FALSE),
        3 => array("type" => "equip", "id" => "3", "selected" => FALSE),
        4 => array("type" => "effort", "id" => "4", "selected" => FALSE),
        5 => array("type" => "comment", "id" => "5", "selected" => FALSE),
        6 => array("type" => "date", "id" => "6", "selected" => FALSE),
        7 => array("type" => "time", "id" => "7", "selected" => FALSE)
        ); ?>
 <form action="?searchview" method="post">
      <div>
        <label for="student">By student:</label>
        <select name="id" id="id">
          <?php foreach (array_reverse($learners) as $learner): ?>
            <option value="<?php htmlout($learner['id']); ?>"><?php
                htmlout($learner['learner']); ?></option>
          <?php endforeach; ?>
        </select>
          <?php include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/logout.inc.html.php'; ?>
      </div>
    <fieldset>
     <legend>Fields to include:</legend>
       <?php foreach($fields as $field):?>
         <div><label for="field<?php htmlout($field['id']); ?>">
         <input type="checkbox" name="fields[]" id="field<?php htmlout($field['id']); ?>" value="<?php htmlout($field['id']);?>"
          <?php
            if($field['selected'])
                {
                  echo 'checked';
                }
           ?>><?php htmlout($field['type']); ?>
              </label>
         </div>
        <?php endforeach; ?>
    </fieldset>
      <div>
        <input type="hidden" name="action" value="searchview">
        <input type="submit" value="Search">
      </div>
    </form>
  </body>
</html>

Then the index.php controller file is,

if (isset($_GET['search']))
{ 
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
  
  include 'search.html.php';
  exit();
}

if (isset($_POST['action']) and $_POST['action'] == 'searchview')
{ 
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
   /* echo "<pre>";
    print_r($_POST);
    echo "</pre>";*/
        /*foreach($_POST['fields'] as $k => $v):
        $col[] = $v;
        endforeach;
    $build = "array("; 
    foreach($_POST['fields'] as $key => $value):
        $build .= "'" . $value . "' => \$row['" . $value . "'], ";
        endforeach; 
        $build = substr($build, 0, -2);
        $build .= ")";
        echo $build . "<br>";*/
        $select = implode(", ", $_POST['fields']);
        //echo $select;
   try
  {
    $sql = "SELECT $select FROM notes WHERE 
    userid = :id";
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
    }
    
  catch (PDOException $e)
  {
    $error = 'Error fetching by fields list of notes.';
    include 'error.html.php';
    exit();
  }

  $result = $s->fetchAll(PDO::FETCH_ASSOC);
  
     try
  {
    $sql = "SELECT learner FROM learners WHERE 
    id = :id";
    $s = $pdo->prepare($sql);
    $s->bindValue(':id', $_POST['id']);
    $s->execute();
    }
    
  catch (PDOException $e)
  {
    $error = 'Error fetching by fields list of notes.';
    include 'error.html.php';
    exit();
  }
       
    $learner = $s->fetchAll();
    
  include 'searchview.html.php';
  exit();
}

I think the problem may be in the HTML file. I am not sure if these two lines are correct or compatible.

 <form action="?searchview" method="post">
........
 <input type="hidden" name="action" value="searchview">

I thought they should direct to the index.php file at

  if (isset($_POST['action']) and $_POST['action'] == 'searchview')
    { 
...

If anyone has any ideas I would be very grateful.
Thanks,
Shane

I have started a new thread as the question is no longer the same as the title.

I can be found here, http://www.sitepoint.com/community/t/html-form-not-directing-to-index-php-on-submit/113462

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.