mercredi 15 janvier 2020

Fetch data from database into checkbox and get the selected values PHP

I am newbie in both PHP and Mysql. I have a table in Mysql named "has" which i store the physical alignments of customers . There are two attributes CustomerID and PyhsicalAilmentName . In sign up screen i want user to select them in a checkbox. I am able to fetch the phsical alignments from database into checkbox with this form code.

   <form action="includes/signup.inc.php"  style="border:1px solid #ccc;width: 50%;margin: 0 auto" method="post" >
            <div class="container" >
            <h1>Sign up</h1>
            <p>Please fill in this form to create an account.(Your username should start with "C_")</p>

                <hr>
            <input type="text" name="username" placeholder="Name">
            <input type="text" name="user_last_name" placeholder="Last Name">
            <input type="text" name="uid" placeholder="Username">
            <input type="password" name="pwd" placeholder="Password">
            <input type="password" name="pwd-repeat" placeholder="Repeat Password">
            <input type="text" name="user_weight" placeholder="Weight(in terms of kilogram)">
            <input type="text" name="user_length" placeholder="Length(in terms of cm)">
            <input type="text" name="user_age" placeholder="Age">

                <p> Phsical Alignments</p>
                <?php
                    $sql = "select Name from physical_ailment";
                    $result = mysqli_query($conn,$sql);
                    $i = 0;

                    while($db_row = mysqli_fetch_array($result)){
                        ?>
                        <input type="checkbox" name="check_list[]"> <?php
                            echo $db_row["Name"]; ?> <br>
                        <?php
                        $i++; }
                        ?>


            <select name="selected_mem_type" >

                <?php
                    $sql = "select Name from membership_type";
                    $result = mysqli_query($conn,$sql);
                    $i = 0;

                    while($DB_ROW = mysqli_fetch_array($result)){
                        ?>
                        <option>
                            <?php echo $DB_ROW['Name']; ?>
                        </option>
                        <?php
                            $i++;} ?>
            </select>
            <input type ="text" name="user_card_no" placeholder="Credit Card No">
            <input type="date" name="user_card_expire_date" placeholder="Expire Date of Card">
            <button type="submit" class="signupbtn" name="signup-submit"> Signup </button>
                <button type="submit" class="signupbtn" name="home-submit"> Home </button>
            </div>
        </form>

Problem is when i intended to get the selected ones with $_POST['check_list']via foreach loop it prints "on" according to number of selected checkboxes. If user selects 3 checkboxes in $_POST['check_list'] there are 3 elements which is "on" . When I select 2 things lets say , and print the $_POST['check_list'] with print_r it outputs Array ( [0] => on [1] => on [2] => on ) I search a lot but couldn't manage to find solution. Appreciate any help thank you for interest.




Aucun commentaire:

Enregistrer un commentaire