2

I am stuck on a script and need another pair of eyes to see if I am missing something. The script is for a bookshop. When a student number is in-putted and searched for the student is displayed with the books that he is suppose to get for each subject. The student, course and book data comes from a MySQL database.

This is all done with this script:

<?php
    if (isset($_POST['submit'])){
           $btnClick = $_POST['submit'];

           switch($btnClick){
              case "Logout" :
                    session_destroy();
                    header("location:index.php");
              break;
              case "Search" :
                  $Validate = $_POST['txtStud'] ;
                  $StudNr = ValidateTxt($Validate);
                  $showStud =  findStud($StudNr);
                  $cid = $showStud[4];
                  $showBooks = findBooks($cid);
                   echo "<form action='issue_book.php' method='post'>";;
                   echo "<table class='table3'>";
                   echo "<tr>";
                   echo "<td>" . $showStud[0] . " " . $showStud[1] . " " . $showStud[2] ."</td>";
                   echo "</tr>";
                   echo "<tr><td></td><td>" . $showStud[3] . "</td></tr>";
                   $array_count = count($showBooks);
                   $num = 0;
                        while ($num != $array_count) {
                           $bookNum = $showBooks[$num]['bid'];
                           echo $bookNum . "<br>";
                           echo "<tr><td>" . $showBooks[$num]['bid'] . "</td>" . "<td>" . $showBooks[$num]['bname'] . "</td>" ;
                           echo "<td><input type='checkbox' name='booknum[]' value='<?php echo $bookNum; ?>'></td></tr>";
                            $num++;
                        }
                   echo "</table>";
                   echo "<br>";
                   echo "<table class = 'table3'>";
                   echo "<tr><td></td><td><input type='submit' name='submit' value='Issue'></td>
                        <td><input type='submit' name='submit' value='Clear'></td></tr>";
                   echo "</form>";
             break;
             case "Issue": 

                 $mybooks = $_POST['booknum'];
                  $h = count($mybooks);
                  echo $h . "<br>";
                  print_r ($mybooks);

             break;
      }
}
?>

At the bottom of the dynamic created data there is 2 buttons. When I click on the Issue button I am presented with this data.

This comes from the code as it is in the script at this moment. I want to send the data from here to the database.

Array ( [0] => [1] => [2] => )

An empty array?? Not sure what happened to the names that I assigned each check box??

I tried to adapt my script according to this forum post Check box link

I am not sure where I am missing something.

Community
  • 1
  • 1
StBlade
  • 287
  • 6
  • 18
  • 1
    Take a step back. Look at the HTML you are generating. Is that the HTML you expect? If not, show us that and don't worry about the data being submitted. If it is, then show us that and don't worry about the PHP generating it. – Quentin Jun 03 '13 at 13:38
  • What output do you get if you select some checkboxes? – Jim Jun 03 '13 at 13:42
  • They're checkbox elements. If I remember well, checkbox on PHP comes `on` when they're submitted checked and empty when not. Check this. – DontVoteMeDown Jun 03 '13 at 13:43
  • @Jim Even if I check some of the boxes I end up with the above output. – StBlade Jun 03 '13 at 13:49
  • @Quentin. The HTML that is generated is what I am looking for, I am stuck on it not working as I thought. – StBlade Jun 03 '13 at 13:50
  • @DontVoteMeDown — That's only if they don't have a value – Quentin Jun 03 '13 at 13:52
  • @StBlade — Then **show us the HTML** and not the PHP that generates it. – Quentin Jun 03 '13 at 13:52

1 Answers1

5

This is because you have a syntax error here

echo "<td><input type='checkbox' name='booknum[]' value='<?php echo $bookNum; ?>'></td></tr>";
                                                         ^php tags are opened ^ 

You are already printing your table inside php tags, you cannot open other tags

value='<?php echo $bookNum; ?>

This is why your array's values are empty but keys exists. You just need to concatenate

echo "<td><input type='checkbox' name='booknum[]' value='".$bookNum."'></td></tr>";
Fabio
  • 23,183
  • 12
  • 55
  • 64
  • You are awesome!!! I have been staring at this script for the better part of a day and missing that!! Thank you so much!! It is working great now!!! – StBlade Jun 03 '13 at 13:55
  • @StBlade You are welcome dude. If the answer was correct don't forget to accept it to help people in the future with similar problems – Fabio Jun 03 '13 at 13:57
  • It's now giving me the answer that I was looking for!! Thanks again!! – StBlade Jun 03 '13 at 14:00