Having problem in Passing Values through Checkbox

Hi There,

I am having some problem with my code.

Actually I am trying to get data from user using checkbox selections

                                                                            <input type="checkbox" name="rel[]" id="rel[]" value="abc1" />abc1<br />
                                                                            <input type="checkbox" name="rel[]" id="rel[]" value="abc2" />abc2<br />
                                                                            <input type="checkbox" name="rel[]" id="rel[]" value="abc3" />abc3<br />
                                                                            <input type="checkbox" name="rel[]" id="rel[]" value="abc4" />abc4<br />

I am using post method to get data…something like this:


if($_POST['rel'])
				{
					$aDoor = $_POST['rel'];
				
					for($i = 0; $i < sizeof($_POST['rel']); $i++)
					{
					  print_r($aDoor[$i] . " ");
					}
}

And result is : A

I’ve tried by selecting one/multiple checkboxes.

In case I select 3 it shows

Arr

and in case I select 5

Result is Array

Please let me know if i mistook somewhere as its very simple code.

Or its a server problem,

how can i fix it?

Thanks for your kind consideration.

I tested your code and didn’t have any issues with it, could you please add the following code below in your script and let us know what it outputs when you submit the form.

if (isset($_POST['rel']) && is_array($_POST['rel'])) {
    echo 'Is an array';
    echo '<pre>';
    print_r($_POST['rel']);
    echo '</pre>';
} else {
    echo 'Not an array';
}

I tested and it worked as expected. Here is the script I used (chkbox.php):

<?php
   if($_POST['rel'])
   {
	   $aDoor = $_POST['rel'];
                
	   for($i = 0; $i < sizeof($_POST['rel']); $i++)
	   {
		   echo "$i: ";
		   print_r($aDoor[$i]);
		   echo "<br/>";
	   }
   }  
?>
<form method="post" action="chkbox.php">
<input type="checkbox" name="rel[]" id="rel[]" value="abc1" />abc1<br/>
<input type="checkbox" name="rel[]" id="rel[]" value="abc2" />abc2<br/>
<input type="checkbox" name="rel[]" id="rel[]" value="abc3" />abc3<br/>
<input type="checkbox" name="rel[]" id="rel[]" value="abc4" />abc4<br/>
<input type="submit" value="Send" />

BTW, you don’t need print_r() for the output. Use this instead:

echo “$i: “.$aDoor[$i].”<br/>”;

Thanks for your replies.

Hi Chris,

Result is: Not an array

Any advice…??

Could you please create a php info file on your server so we can check your php.ini settings as i tried a combination of settings but couldn’t replicate your issue. On your server create a file called phpinfo.php and put the following in it:

<?php

phpinfo();

?>

Hi Chris,

I’ve made phpinfo.php

Here’s the link to view further.

http://www.rksmatrimony.com/phpinfo.php

I setup PHP 5.2.17 on my localhost with the same settings as what your php.ini file outputted but again i was unable to replicate the issue, the only thing i can do is offer you 2 solutions which might work but there is no guarantee.

  1. Re-compile your current PHP build but leave it with stock settings
  2. Upgrade to PHP 5.3.x or PHP 5.4

Thanks for your help Chris!!..

Much Appreicated!!

I will try both the way you offered…

Thanks Again!!

How about showing us the entirity of your HTML page.

Wait, no, nevermind…

Why are you using print_r? You should just be using print, or echo.

                  print_r($aDoor[$i] . " ");

                  print($aDoor[$i] . " ");

Hi Star,

I’ve tried with echo…too but the problem was same.

Here’s the HTML version


&lt;form id="formmypartnerdet" name="formmypartnerdet" action="redirect.php" method="post"&gt;

									  $myrel = explode("/",$religion);
									  //$mrealrel = count($myrel);
									  $z=0;
									  $sql5 = "SELECT * FROM `tbl_name`";
									  $result5 = dbQuery($sql5);
										
										while($row5 = dbFetchAssoc($result5)) {
										extract($row5);
										if(isset($myrel[$z])== $religion_name)
										{
											//echo $myrel[$z];
										?&gt;
                                        &lt;input type="checkbox" checked="checked" value="&lt;?php echo $religion_name;?&gt;" name="rel[]" id="rel[]" /&gt;
                                        &lt;?php echo $myrel[$z];?&gt;&lt;br /&gt;
                                        
										&lt;?php
										}
										else
										{
										?&gt;
                                        &lt;input type="checkbox" value="&lt;?php echo $religion_name;?&gt;" name="rel[]" id="rel[]" /&gt;&lt;?php echo $religion_name;?&gt;&lt;br /&gt;
                                        &lt;?php
										}
										$z++;
										}
										
&lt;/form&gt;

Besides, this, is there any other way to pass multiple selected values from user…instead of it.

Thanks in advance for your help!

if(isset($myrel[$z])== $religion_name)

well thats not gonna ever be true unless you name one of your religions “1” or true. (isset will return a boolean.)

Try this:


foreach($_POST['rel'] AS $value) { echo $value; }

Because of how checkboxes work (If they arnt checked, they dont send anything), it should give you what you want.