HTML form not directing to index.php on submit

Hi,
I have trouble with a form that isn’t directing to the controller index.php file when I hit submit.

<?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

the action attribute of the form tells the form which URL to submit to. It does not set a value in $_POST. Change your action to be “?action=searchview” and check for $_GET['action'].

Hi,
I have changed the beginning of the form HTML to,

<!-- <form action="?searchview" method="post"> -->
     <form action="?action=searchview" method="post">
        <div>
            <label for="student">By student:</label>

The bottom of the form is,

      <div> 
        <input type="hidden" name="action" value="searchview"> 
     
        <input type="submit" value="search">
      </div>
</form>

and the index.php is now,

//if (isset($_POST['action']) and $_POST['action'] == 'searchview')
if (isset($_GET['action']) and $_GET['action'] == 'searchview')
{ 
  include $_SERVER['DOCUMENT_ROOT'] . '/artgibney/includes/db.inc.php';
  //echo("sjdndnn");
    /*echo "<pre>";

But still nothing happens when I hit the submit button on this form.
There are no additional errors in the error_log file.
Thanks,
Shane

Have you enabled error reporting?

Eh! no!
But I usually get errors in error_log so I perfume that is working.
How do I check to see if it working?
Thanks,
Shane

I have also tried this for the form,

 <form action="?" 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>

and this for the index.php

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

which i really thought should work.
I am totally confused.
Thanks,
Shane

Is this up online somewhere? I want to play with the page in my browser.

Things that appear on the URL are referenced through $_GET. Input elements of a form with it’s method set to ‘post’ are referenced through $_POST. When you submit the form, you can see that there is the ?action=searchview on the end of the URL in the address bar, meaning it will be in the $_GET array.

Hi,

But why can’t I use $_POST to send ‘searchview’ as well as the form variables?

Well yes it is online but the login section is another issue I have to work on. I can’t even change passwords.

www.artgibney.com but that won’t be much use.

Thanks,
Shane

You could, if you really felt the absolute need to, send it using an <input type='hidden' ....> , and then it would appear in the post array.

Yes that is what i was trying to do above in post #7 with

<input type="hidden" name="action" value="searchview"> 

And it should work. I can’t see why this doesn’t.

Could you give us a dummy email/pass?

1 Like

You can’t have a form inside a form…Close one form before you start another. That’s why it’s not submitting.

Done, I PM’d you.
Thanks

Yes I see it now. Of course, I have actually got two forms on the page but as you say I have to close one before opening the other. Can’t believe i didn’t see this.
Will try to fix it now and let you know how I get on.
Thanks,

On second thoughts, I can have a options or pull-down menu and an array of checkboxes all inside the same form tags, cant’ I?
Thanks,
Shane

You can as long as they have a diffferent name. You aren’t restricted.

This is the actual code from Kevin Yank’s Sitepoint book that I am trying to adapt.

<?php include_once $_SERVER['DOCUMENT_ROOT'] .
    '/includes/helpers.inc.php'; ?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title><?php htmlout($pageTitle); ?></title>
    <style type="text/css">
    textarea {
      display: block;
      width: 100%;
    }
    </style>
  </head>
  <body>
    <h1><?php htmlout($pageTitle); ?></h1>
    <form action="?<?php htmlout($action); ?>" method="post">
      <div>
        <label for="text">Type your joke here:</label>
        <textarea id="text" name="text" rows="3" cols="40"><?php
            htmlout($text); ?></textarea>
      </div>
      <div>
        <label for="author">Author:</label>
        <select name="author" id="author">
          <option value="">Select one</option>
          <?php foreach ($authors as $author): ?>
            <option value="<?php htmlout($author['id']); ?>"<?php
                if ($author['id'] == $authorid)
                {
                  echo ' selected';
                }
                ?>><?php htmlout($author['name']); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <fieldset>
        <legend>Categories:</legend>
        <?php foreach ($categories as $category): ?>
          <div><label for="category<?php htmlout($category['id']);
              ?>"><input type="checkbox" name="categories[]"
              id="category<?php htmlout($category['id']); ?>"
              value="<?php htmlout($category['id']); ?>"<?php
              if ($category['selected'])
              {
                echo ' checked';
              }
              ?>><?php htmlout($category['name']); ?></label></div>
        <?php endforeach; ?>
      </fieldset>
      <div>
        <input type="hidden" name="id" value="<?php
            htmlout($id); ?>">
        <input type="submit" value="<?php htmlout($button); ?>">
      </div>
    </form>
  </body>
</html>

Shane

I think they do.

I’ll let @StarLion take it from here. Now that the invalid HTML is sorted out (validating should be the first step in debugging) he can help you out with any other issues that might appear. I don’t quite know enough with PHP to feel comfortable telling people what to do.