-2

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.

1
  • After 10 years of content generation and millions of questions, most basic programming questions have at least 5 pages that express how to solve a given issue. Please toil a bit more when searching for solutions before asking a new question. Commented Jan 15, 2020 at 22:55

1 Answer 1

1

If you don't supply a value attribute to your checkbox in your HTML, it will default to on in most browsers to let you know it had been checked.

So if you're making checkboxes asking people to check their 3 favorite fruits.

<input type="checkbox" name="check_list[]"> Banana <br>
<input type="checkbox" name="check_list[]"> Apple <br>
<input type="checkbox" name="check_list[]"> Orange <br>

If all 3 are checked, you will have Array ( [0] => on [1] => on [2] => on )

Now if you add the value attribute

<input type="checkbox" name="check_list[]" value="banana"> Banana <br>
<input type="checkbox" name="check_list[]" value="apple"> Apple <br>
<input type="checkbox" name="check_list[]" value="orange"> Orange <br>

If all 3 are checked you'll have : Array ( [0] => banana [1] => apple [2] => orange )

You can read more about that here : https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Value

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.