Results 1 to 5 of 5

Thread: [RESOLVED] How to receive multiple checkboxes values on a page

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Resolved [RESOLVED] How to receive multiple checkboxes values on a page

    Hi I have a following code in html, which is asking for cities you have visited and a submit button

    Code:
    <div>
    
    <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>" >
    
    <h3> SElect the cities you have visited </h3>
    Peshawar: <input type="checkbox" name="chk[]" value="Peshawar" /><br />
    Islamabad: <input type="checkbox" name="chk[]" value="Islamabad" /><br />
    Karachi: <input type="checkbox" name="chk[]" value="Karachi" /><br />
    Charsadda: <input type="checkbox" name="chk[]" value="Charsadda" /><br />
    Swabi: <input type="checkbox" name="chk[]" value="Sawabi" /><br />
    Mardan: <input type="checkbox" name="chk[]" value="Mardan" /><br />
    <input type="submit" value="Send" />
    
    <h4> I visited the followig cities: </h4>
    </form>
    </div>
    On the same page i have the php code which takes the checkboxes values:
    Code:
    <?php
    
        $chk = $_POST['chk'];
        foreach($chk as $chek)
        {
            echo $chek. "<br>";
        }
    ?>
    If the user checks-in 2 cities so 2 cities names will be displayed on the page i.e. superglobals.php

    Question

    My question is that When the php code receives all the checkboxes values, so how it differentiates that which checkbox was checked by the user and which was remained unchecked. Like in my case the user selects 2 checkboxes so only those two will be displayed on page. So how php understands that how many checkboxes have been checked in by the user.

    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: How to receive multiple checkboxes values on a page

    My question is that When the php code receives all the checkboxes values
    Your PHP code actually doesn't receive all the values.

    When you fill out and submit a form, your browser is responsible for sending the form data to the form's "action" target. In the case of checkboxes, if it's not checked, the value doesn't get sent. So when your PHP shows 2 values, that's all the information that it received; it doesn't know that there were any other values.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to receive multiple checkboxes values on a page

    Meaning thereby that if we will select 3 checkboxes, so only three will be sent to the target form and it must be in form of array. right?

  4. #4
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: How to receive multiple checkboxes values on a page

    Quote Originally Posted by ADQUSIT View Post
    Meaning thereby that if we will select 3 checkboxes, so only three will be sent to the target form and it must be in form of array. right?
    That is correct.

    When I need to know the value of each and every selection to update a db table I use two arrays. One array has all of the questions (or would-be checkboxes) and the other is the form submission. I go through each entry in the full array and set the value from the post array, default would be 0 or false. Then I have a fully inclusive array.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2010
    Posts
    965

    Re: How to receive multiple checkboxes values on a page

    Thank you very much dclamp and samba for you guidance and devotion of time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width