-
Jan 13th, 2015, 09:27 AM
#1
Thread Starter
Fanatic Member
[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.
-
Jan 14th, 2015, 12:06 PM
#2
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.
-
Jan 22nd, 2015, 08:53 AM
#3
Thread Starter
Fanatic Member
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?
-
Jan 22nd, 2015, 07:33 PM
#4
Re: How to receive multiple checkboxes values on a page
Originally Posted by ADQUSIT
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.
-
Jan 23rd, 2015, 12:11 AM
#5
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|