1

I am trying to build a simple form which queries a database, grabs a list of email addresses and then creates a table based on the results. What I would like it to do is retain the checked boxes after a submission but am having trouble figuring it out based on the way I've created my table. I can do it no problem if I manually build the table but that defeats the purpose. Here is the code I am working with, again the only change I would like it to do is retain the checked boxes.

<html>
    <head>
        <title>Test</title>
        <link rel="stylesheet" type="text/css" href="style/style.css"/>
    </head>
    <body>
    <?php include('include/connect.php'); ?>
        <h1>This is a test</h1>
        <div class="emailform">
            <form action="" method="post">
                <table id="emails">
                    <?php
                        while($row = $result->fetch_assoc()) {
                            unset($email);
                            $email = $row['Email'];
                    ?>
                    <tr><td><input type="checkbox" name="select[]" value="<?php echo $email;?>"/><?php echo $email; ?></td></tr>
                    <?php
                        }
                    ?>
                </table>
                <br/><br/>
                <input id="manual" type="text" name="select[]"><br/><br/><br/>
                <button type="submit" name="SubmitButton">Select Email Addresses</button>
            </form> 
        </div>
        <?php
            if(isset($_POST['SubmitButton'])){
                if(isset($_POST['select'])){
                    $shift = $_POST['select'];
                    if (count($shift) > 1 ){
                        $list = implode(", ", $shift);
                        echo $list;
                    } else {
                        echo "$shift[0] <br/>";
                    }
                }
            }
        ?>
    </body>
</html>

Help would be appreciated, thanks

1
  • 1
    You may want to add a column or table (based on your application) to the database to see if it's been checked. Otherwise look at the input checked attribute, I'm not sure which part you are struggling with. Sorry for W3schools Commented Jun 28, 2016 at 17:36

2 Answers 2

1

Just check if the current email in the loop exists in $_POST['select'], if it is, you check it, if it is not, clear the check. This check will be displayed in the input checkbox as <?php echo $checked;?> :

 <?php
     while($row = $result->fetch_assoc()) {
         unset($email);
         $email = $row['Email'];
         // IF EMAIL EXISTS IN $_POST, CHECK IT.
           $checked = "";
           if(isset($_POST['select'])){
                $shift = $_POST['select'];
                $list = implode(", ", $shift);
                if (strpos($list,$email)===false)
                     $checked = "";         // EMAIL NOT IN $_POST.
                else $checked = "checked";  // EMAIL IS IN $_POST.
           }
 ?>
 <tr><td><input type="checkbox" name="select[]" <?php echo $checked;?>
          value="<?php echo $email;?>"/><?php echo $email; ?></td></tr>
 <?php
     }
 ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Work's perfectly. I am going to study this code to see what's going on. I don't code in php too often.
1

Check if $row['Email'] isn't empty, then output "checked" attribute.

<?php
while($row = $result->fetch_assoc()) {
unset($email);
$email = $row['Email'];
?>
<tr><td><input type="checkbox" name="select[]" value="<?php echo $email;?>"<?php if($row['Email'] != false) { echo ' checked'; } ?>><?php echo $email; ?></td></tr>
<?php
}
?>

2 Comments

I guess the issue is because the name is an array "select[]" so determining the box I want to remain checked is a little confusing
Haha, i need to get myself that comment anywhere privilege ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.