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
https://forums.phpfreaks.com/topic/195448-checkbox-not-return-the-value/
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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.