1

I have a table and it contains more than one column, and one of columns is to check/select all checkboxes in my table rows, actually I can select one or multiple checkboxes if I want from the checkboxes that are in the rows.

I have searched and found this result: multiple checkboxes with php in table but it's not helpful, and I couldn't understand it completely.

What I want from my code to do is: When I select one or multiple checkboxes, then when I click on (Publish) button -the code for the button is down below within the page code- the page should receive the selected values from the checkboxes and send them to the same page (article?do=pub&articleid='.$fileid)


This is the code:

<?php 
    $Title = 'Manage Approved Articles';
    include 'init.php';
    $AdminName = $_SESSION['username'];
    // Check if user is the Admin or not
    $stmt = $con->prepare('SELECT Username FROM users WHERE Username = ? AND isAdmin = 1');
    $stmt->execute(array($AdminName));
    $count = $stmt->rowCount();
    if($count > 0) { }  else {
        header('Location: ../index');
        exit();
    }
    ?>
        <section class="Cust-container">
    <?php
      $do = isset($_GET['do']) ? $_GET['do'] : 'article';
      if($do == 'article') { echo 
      '<h2 class="heading">Manage Articles</h2>'; ?>
      <!-- Manage Articles Page -->
        <table class="admin-article-table">
            <thead>
                <tr>
              <th width="41%">Article Title</th>
              <th width="18%">Category</th>
              <th width="10%">By</th>
              <th width="10%">Publish
                <input style="height: 20px; width: 20px;margin-left: 7px"
                        type="checkbox"
                        id="checkAll">
              </th>
              <th width="21%">Options</th>
                </tr>
            </thead>
            <tbody>
        <?php
      // retrieving data from db
      $stmt = $con->prepare('SELECT * FROM uploads WHERE Approved = 1');
      $stmt->execute();
      $rows = $stmt->fetchAll();

      if(empty($rows)) { echo
      '<tr>
        <td class="empty" colspan="5">There\'re No Approved Articles</td>
       </tr>'; 
      } else {
      foreach ($rows as $row) {
      $fileid       = $row['FileID'];
      $filedir      = $row['FileDirectory'];
      $catID        = $row['CatID'];
      $F_name       = array_shift(explode('.', $row['FileName']));
      $full_fname = $row['FileName']; 
      $uploaderId = $row['User_ID']; echo 
      '<tr>
        <td>' . $row['ArticleTitle'] . '</td>';?>
        <?php
            $stmt = $con->prepare('SELECT * FROM cats WHERE ID = ?');
            $stmt->execute(array($catID));
            $rows = $stmt->fetchAll();
            if(empty($rows)) {
                echo "<td class='empty'>No Declared Category</td>";
            } else {
                foreach ($rows as $row) {
                    $category = $row['Category'];
                } echo '
                        <td>'.$category.'</td>
                ';
            }
        echo '<td>';?>
            <?php
            $stmt = $con->prepare('SELECT FullName FROM users WHERE UserID = ?');
            $stmt->execute(array($uploaderId));
            $rows = $stmt->fetchAll();
            foreach ($rows as $row) {
                echo $row['FullName'];
             } echo '
             </td>
                <td>
                    <input type="checkbox" name="Publ[]"
                    value="'.$fileid.'" class="chk" required>
                </td>
                    <td>';?>
                <?php echo '
            <a class="option-del" style="margin-left: 12%;float:left" href="?do=delete&fileid='.$fileid.'&fullName='.$full_fname.' "onclick="return ConfirmContin(this);">Delete</a>
            <a class="option" style="float:left;margin-right: -30px" href="?do=unapprove&fileid='.$fileid.'&filename='.$F_name.'&fullnm='.$full_fname.'" onclick="return ConfirmContin(this);">Unapprove</a>
            <br><br><br>
            <form method="post" action="?do=updateCat&fileid='.$fileid.'">
            <select name="cats">';
                 $stmt = $con->prepare('SELECT * FROM cats');
                 $stmt->execute();
                 $rows = $stmt->fetchAll();
                 if(empty($rows)) {
                    echo "<option>-- No Categories Found --</option>";
                 } else {
                    echo "<option value='0' Title='delete the article from its chosen category'>Make as Uncategorized</option>";
                    foreach ($rows as $row) {
                        $catid   = $row['ID'];
                        $catname = $row['Category'];
                        echo '<option value='.$catid.'>'.$catname.'</option>';
                    }   
                 } echo '</select><input type="submit" style="width:130px;height:40px;border-color:#777;background-color:#444;color:#FFF" value="Update"></form>
        </td>
       </tr>
       ';  
       }
      }
        ?>
            </tbody>
            <tr><td colspan="3"></td>
            <td>
                <input type="submit" value="Publish" name="publish"
                style="width:130px;height:40px;border-color:#090B64;
                background-color:#475590;color:#FFF">
            </td>
            <td></td></tr>
        </table>
        </section>
        <?php
      } elseif ($do == 'pub') {
            // Some Code for handling the sent values (sent by clicking on Publish button) from the selected checkboxes that exists in the table rows
        } else {
            header('Location: ../index');
            exit();
        } ?>

    <?php 
    include $tpl . 'footer.php';
    ?>

This is a screen shot of the table:

Table for checking multiple checkboxes

I am a beginner and I don't know a lot about php and handling requests (get and post). if I didn't clarify my question enough please ask me in which point I didn't, so you can fully understand my problem and help me solving it hopefully.

So much of thanks.

Community
  • 1
  • 1
Maohammed Hayder
  • 193
  • 1
  • 3
  • 13
  • Could you explain what exactly you want (ie give a hypothetical example where this would actually work) – Cullub Oct 17 '16 at 20:47
  • @cullub I want to get the values of the selected checkboxes when I click the button (Publish) , and I found it difficult because the button and the checkboxes are separated from each others so I can't put them in a form tag , because the inputs (Checkboxes) are inside the table and the button is in different row of the table. So the simple question is : How I can get the checkboxes values when I click on Publish button : `` – Maohammed Hayder Oct 18 '16 at 02:14

1 Answers1

0

What you need is a form.

W3Schools does a pretty good job of describing how forms work, but I'll go over it quickly here as well.

The HTML <form> element defines a form that is used to collect user input. You'd put a form around your table, so every input inside your table would get sent when you click your submit button (in this case publish). If you're using a GET, the data will be sent in the link text after the question mark, and if you use a POST, it won't use the link address. Basically if you are going to change something (like publish your article), you'll want to use POST. (What is the difference between POST and GET?).

You'll access the value of the checkbox after the user clicks submit. When he does that, it'll sent all the information to the form's action value - or if it doesn't have one, then directly to the same page, where you'll do the processing before showing the updated info. E.g. <form action="myActionPage.php" method="POST"> will send all the info inside that form via POST to myActionPage.php. Inside myActionPage.php, you'll access your checkbox values like so: $_POST['checkboxName']. If you want to access multiple checkboxes of the same name, use $_POST['checkboxName'][] - it's an array of the checkboxes. (https://stackoverflow.com/a/4997271/3437608 does an excelent job explaining this)

Once you've gotten the values, check to see whether isset($_POST['checkboxName']) - if true, then checkbox was checked. (How to read if a checkbox is checked in PHP?)

Then you can happily publish the page, and direct the user either back to the admin page you showed, or the page they just published.

Good Luck!

Community
  • 1
  • 1
Cullub
  • 2,306
  • 2
  • 24
  • 44