Jump to content

Checkbox not return the value


robert_gsfame

Recommended Posts

I have this code

<?php $country=$_POST['country'];

if($country[0]=="brazil"){

$brazil="CHECKED";}

 

if($country[1]=="paraguay"){

$paraguay="CHECKED";}

 

if($country[2]=="germany"){

$germany="CHECKED";}

 

if($country[3]=="argentina"){

$brazil="CHECKED";}

 

if($country[4]=="japan"){

$brazil="CHECKED";}

 

?>

<html>

<head>

</head>

<body>

<form name=form1 action=$_SERVER['PHP_SELF'] method=POST>

<input type =checkbox name=country[] value=brazil <?php echo $brazil;?>>Brazil

<input type =checkbox name=country[] value=paraguay <?php echo $paraguay;?>>Paraguay

<input type =checkbox name=country[] value=germany <?php echo $germany;?>>Germany

<input type =checkbox name=country[] value=argentina <?php echo $argentina;?>>Argentina

<input type =checkbox name=country[] value=japan <?php echo $japan;?>>Japan

<input type=submit value=okay>

</form>

</body>

</html>

 

The problem is when i tick on all checkboxes and click on the submit button, i only found that only 3 checkboxes being ticked although i have ticked 4 checkboxes...??Which code is wrong??

 

thx

Link to comment
Share on other sites

I have this code

 

<?php $country=$_POST['country'];
if($country[0]=="brazil"){
$brazil="CHECKED";}

if($country[1]=="paraguay"){
$paraguay="CHECKED";}

if($country[2]=="germany"){
$germany="CHECKED";}

if($country[3]=="argentina"){
$brazil="CHECKED";}

if($country[4]=="japan"){
$brazil="CHECKED";}

?>
<html>
<head>
</head>
<body>
<form name=form1 action=$_SERVER['PHP_SELF'] method=POST>
<input type =checkbox name=country[] value=brazil <?php echo $brazil;?>>Brazil
<input type =checkbox name=country[] value=paraguay <?php echo $paraguay;?>>Paraguay
<input type =checkbox name=country[] value=germany <?php echo $germany;?>>Germany
<input type =checkbox name=country[] value=argentina <?php echo $argentina;?>>Argentina
<input type =checkbox name=country[] value=japan <?php echo $japan;?>>Japan
<input type=submit value=okay>
</form>
</body>
</html>

The problem is when i tick on all checkboxes and click on the submit button, i only found that only 3 checkboxes being ticked although i have ticked 4 checkboxes...??Which code is wrong??

 

thanks

 

my guess would be these lines here:

if($country[0]=="brazil"){
$brazil="CHECKED";}

if($country[1]=="paraguay"){
$paraguay="CHECKED";}

if($country[2]=="germany"){
$germany="CHECKED";}

if($country[3]=="argentina"){
$brazil="CHECKED";}  // <--- Here

if($country[4]=="japan"){
$brazil="CHECKED";}  // <--- Here

 

you have $brazil being checked 3 times, so it only reads 3 checks, not 4 or 5.

Link to comment
Share on other sites

What are you trying to do? This is a bad way of writing your code. First you should use a function like in_array() instead of checking the specific array key for a value because the array will only hold whatever has been checked. That's what is going wrong here.

 

So instead of

if($country[0]=="brazil") 

 

you should have

if(in_array("brazil",$country))

 

my guess would be these lines here:

if($country[0]=="brazil"){
$brazil="CHECKED";}

if($country[1]=="paraguay"){
$paraguay="CHECKED";}

if($country[2]=="germany"){
$germany="CHECKED";}

if($country[3]=="argentina"){
$brazil="CHECKED";}  // <--- Here

if($country[4]=="japan"){
$brazil="CHECKED";}  // <--- Here

 

you have $brazil being checked 3 times, so it only reads 3 checks, not 4 or 5.

 

This too

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.