• 0

[PHP] checkbox array


Question

ok, this is a real brain buster! and i can not figure this out.

there will be 1 - 50 checkboxes, depending on the amount the user has as an option.

lets say the user has 8 checkboxes for now, and for each checkbox selected, it gets displayed.

the values for each, are simply, 1, 2, 3, 4.

heres some code ive been working on, but why isn't it displaying proper checkboxes? if i select 1 - 8, it displays all the numbers. good right? yes. now say i select, 1, 2, 5, 6, 7. i get 1, 2, 5 returned. ahh i can't figure this out..

<?php
echo "array test<p>";

if (isset($_POST['test22'])) {
   for ($i=0; $i<count($_POST['test1']);) {
    echo($_POST[test1][$i]);
     $i++;
}
    echo "<br>test...";
} else {
$self_server = $_SERVER['PHP_SELF'];
echo "<form name=\"form1\" method=\"post\" action=\"$self_server\">";

echo "<br><input type=\"checkbox\" name=\"test1[0]\" value=\"1\">";
echo "<br><input type=\"checkbox\" name=\"test1[1]\" value=\"2\">";
echo "<br><input type=\"checkbox\" name=\"test1[2]\" value=\"3\">";
echo "<br><input type=\"checkbox\" name=\"test1[3]\" value=\"4\">";
echo "<br><input type=\"checkbox\" name=\"test1[4]\" value=\"5\">";
echo "<br><input type=\"checkbox\" name=\"test1[5]\" value=\"6\">";
echo "<br><input type=\"checkbox\" name=\"test1[6]\" value=\"7\">";
echo "<br><input type=\"checkbox\" name=\"test1[7]\" value=\"8\">";

echo "<INPUT TYPE=\"SUBMIT\"  name=\"submit\" value=\"submit\">";
echo "<INPUT TYPE=\"checkbox\"  name=\"test22\" value=\"hahaha\" checked>";
echo "</form>";

}
?>

Link to comment
https://www.neowin.net/forum/topic/386529-php-checkbox-array/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

<?php

echo "array test<p>";

if (isset($_POST['test22'])) {

for ($i=0; $i<8; $i++) {

echo($i . ' - ' . $_POST['test1'][$i] . '<br>');

}

echo "<br>test...";

} else {

$self_server = $_SERVER['PHP_SELF'];

echo "<form name=\"form1\" method=\"post\" action=\"$self_server\">";

echo "<br><input type=\"checkbox\" name=\"test1[0]\" value=\"1\">";

echo "<br><input type=\"checkbox\" name=\"test1[1]\" value=\"2\">";

echo "<br><input type=\"checkbox\" name=\"test1[2]\" value=\"3\">";

echo "<br><input type=\"checkbox\" name=\"test1[3]\" value=\"4\">";

echo "<br><input type=\"checkbox\" name=\"test1[4]\" value=\"5\">";

echo "<br><input type=\"checkbox\" name=\"test1[5]\" value=\"6\">";

echo "<br><input type=\"checkbox\" name=\"test1[6]\" value=\"7\">";

echo "<br><input type=\"checkbox\" name=\"test1[7]\" value=\"8\">";

echo "<INPUT TYPE=\"SUBMIT\" name=\"submit\" value=\"submit\">";

echo "<INPUT TYPE=\"checkbox\" name=\"test22\" value=\"hahaha\" checked>";

echo "</form>";

}

?>

  • 0

&lt;form name="form" method="post" action=""&gt;
&lt;?
if(!empty($_POST['test'])) {
	foreach($_POST['test'] as $number =&gt; $value){
  echo $number;
  echo "&lt;br&gt;";
	}
}else{
	for($a = 0; $a &lt; 50; $a++){
?&gt;
&lt;input type="checkbox" name="test[&lt;?=$a?&gt;]" value="&lt;?=$a+1?&gt;"&gt;
&lt;br&gt;
&lt;?
	}
}
?&gt;
&lt;input type="submit"  name="submit" value="submit"&gt;
&lt;/form&gt;

there ya go, m8!

Your PHP code was a bit messy, the more HTML you use and the less complicated your code will be! :cool: :D

I hope you understand everything.

Edited by PsykX
  • 0

hey thanks tylerc, i modified your lines, and made it the way i wanted it :)

if (isset($_POST['test22'])) {
   for ($i=0; $i&lt;8; $i++) {
    echo($_POST[test1][$i]);

}
    echo "&lt;br&gt;test...";
} else {

thanks, but your code is a little off PsykX :p

it's nice, and thanks for the reply, but not what i was looking for.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.