Asked
Updated
Viewed
183k times

hey peoples
i really need to know how to get the values of a check box array using the post method in PHP.
the checkboxes look as such

<input type="checkbox" name="branchsel[]" value="$data[BRANCH]">$data[BRANCH]<br>

*NOTE: the values are coming form the database

i dont need to worry about checking if that are checked, jsut need to get values, whether its one big string or an array. full explaination is needed with the soltutions

much much appreciated

add a comment
0

8 Answers

  • Votes
  • Oldest
  • Latest
CA
0 0
Answered
Updated

i dont need to worry about checking if that are checked

they are only going to send the values to the server if they are checked.

lets say you have this:

<input type="checkbox" name="fruit" value="apples">apples
<input type="checkbox" name="fruit" value="oranges">oranges
<input type="checkbox" name="fruit" value="peaches">peaches
<input type="checkbox" name="fruit" value="mangos">mangos

If you check apples and oranges,

$HTTP_POST_VARS["fruit"] will be an array containing two values: apples and oranges.

add a comment
0
Answered
Updated

yeah nah i need to get the values from a checkbox array

i know how to post them to another page, but its putting them/sorting them into an array or sorting through the array

please

add a comment
0
CA
0 0
Answered
Updated

will this help?

<html>
<head>
<title>checkbox help</title>
</head>
<?

if (isset($HTTP_POST_VARS)) {

        $fruit = $HTTP_POST_VARS["fruit"];

        echo("Fruits chosen: " . count($fruit) . "<br><br>");

        if (count($fruit)>0) {
                echo("You chose the following fruits:<br>");
        }

        for ($i=0; $i<count($fruit); $i++) {
                echo( ($i+1) . ") " . $fruit[$i] . "<br>");
        }
}
?>
<body bgcolor="#ffffff">

<form method="post">
Choose a fruit:<br><br>
<input type="checkbox" name="fruit[]" value="apples">apples <br>
<input type="checkbox" name="fruit[]" value="oranges">oranges <br>
<input type="checkbox" name="fruit[]" value="peaches">peaches <br>
<input type="checkbox" name="fruit[]" value="mangos">mangos<br>
<input type="submit">
</form>

</body>
<html>
add a comment
0
Answered
Updated

Apologies for posting to such an old thread, but my google search brought me here and I found the info here helpful, although slightly flawed and a little dated. Others may arrive here also needing help so I will contribute my 2cents.

I found Cafu's post above to be a good jumping off place. $HTTP_POST_VARS is now deprecated and should be updated to the superglobal $_POST. I updated the code and tweaked it a bit as I was learning how the code handles checkboxes. My notes are included in the code.

<!--  
Instructive notes: 
	1. to use the     if (isset($_POST['submit']))    construct, the submit button must have a name defined as 'submit' 
	2. if each of the checkboxes has been named with the same name (ie 'fruit')  followed by a pair of square brackets '[]', 
when the form is submitted, the $_POST superglobal will have a variable named $_POST['fruit'] that represents an 
array containing array elements from only those checkboxes that have been checked. 
	3. count() returns the number of elements in an array.  here it returns '0' if no checkboxes are checked.  This could be  
because $_POST['fruit'] is not set, or because $_POST['fruit'] represents an array that has been set but has no elements. 
if we change the if statement to   if (isset($_POST['fruit'])), the statements in the 'if' block are never executed,  
so $_POST['fruit'] is not set and must evaluate to null.  count() accurately returns '0' when its parameter is null. 
	4. the value of each element in the array $_POST['fruit'] is determined by the value attribute of each of the checkboxes. 
	5. note that in the absence of an 'action = "some_script.php" attribute in the form element, the browser reloads this script when the submit button is clicked. 

--> 
<html> 
<head> 
<title>checkbox help</title> 
</head> 
<?php 
if (isset($_POST['submit'])) { 
        $fruit = $_POST["fruit"]; 
	$how_many = count($fruit); 
        echo 'Fruits chosen: '.$how_many.'<br><br>'; 
        if ($how_many>0) { 
                echo 'You chose the following fruits:<br>'; 
        } 
        for ($i=0; $i<$how_many; $i++) { 
                echo  ($i+1) . '- ' . $fruit[$i] . '<br>'; 
        } 
		echo "<br><br>"; 
} 
?> 
<body bgcolor="#ffffff"> 
<form method="post"> 
Choose a fruit:<br><br> 
<input type="checkbox" name="fruit[]" value="apples">apples <br> 
<input type="checkbox" name="fruit[]" value="oranges">oranges <br> 
<input type="checkbox" name="fruit[]" value="peaches">peaches <br> 
<input type="checkbox" name="fruit[]" value="mangos">mangos<br> 
<input type="submit" name = "submit"> 
</form> 
</body> 
<html>

Larry Fundell

add a comment
0
Answered
Updated

Thank you for that update! It is exactly what I was looking for, and what a coincidence that it was posted by somebody from my hometown of La Crosse.

add a comment
0
Answered
Updated

Or you could use "foreach":

foreach($_POST['check']  as  $value)  {
$check_msg .= "Checked: $value\n";
} 

The accompanying form code would be:

<input  type="checkbox"  name="check[]"  value="blue_color">  Blue<br>
<input type="checkbox" name="check[]" value="green_color"> Green<br>
<input type="checkbox" name="check[]" value="orange_color"> Orange<br> 

It that doesn't clarify it enough --

https://www.kirupa.com/web/php_contact_form3.htm

add a comment
0
Answered
Updated

is there anyway to get all the checkboxes through a post, even the ones that aren't checked? I too am from La Crosse 🙂 .

add a comment
0
Answered
Updated

I could lend a hand? I have a table where values sakan...I would like to do is power through checkboxes have the possibility to use the same value for multiple records.... I am a rookie.!

Example I have a list of students who were loading a database, and these students are assigned a rating of another database, as sometimes the ratings are the same selection is engorosso scores again and again....

case 4:
/ / SCREEN FOR QUALIFYING

print "Input captured <font face=arial size=3> </ font> <br>";
print "<table border=1>";
print "<TR borderColor = #bgColor = ffffff #7799bb> ";
print "<td> <font face=arial size=3> CATEGORY </ font> </ td>";
print "DESCRIPTION <td> <font face=arial size=3> </ font> </ td>";
print "<td> <font face=arial size=3> UNIT </ font> </ td>";
print "<td> <font face=arial size=3> PRICE MN </ font> </ td>";
print "<td> <font face = arial size = 3> PRICE USD </ font> </ td> ";
print "<td> <font face=arial size=3> AMOUNT </ font> </ td>";
print "<td> <font face=arial size=3> RAMA </ font> </ td>";
print "</ tr>";
$ result2 = mysql_query ( "SELECT id_insumo, category, des_insumo, unity, precio_unita river precio_unitario_usd, can not branch from tb_insumo where contract = $ id_contrato ORDER BY category, des_insumo");
while ($ row2 = mysql_fetch_array ($ result2, MYSQL_ASSOC))
(
$ homologa_completo = 0;

print "<TR>";
print "<td> <font face=arial size=3> $ row2 [category] </ font> </ td>";
print "<td> <font face=arial size=3> $ row2 [des_insumo] </ font> </ td>";
print "<td> <font face=arial size=3> $ row2 [drive] </ font> </ td>";
print "<td> <font face=arial size=3>. number_format ($ row2 [precio_unitario], 2,. ,)."</ font> </ td> ";
print "<td> <font face=arial size=3>. number_format ($ row2 [precio_unitario_usd], 2,. ,)."</ font> </ td>";
print "<td> <font face=arial size=3>. number_format ($ row2 [can not], 2,. ,)."</ font> </ td>";
if ($ row2 [branch]! = "none")
(
print "<td> <font face=arial size=3> <A HREF = JavaScript & #058; popUp (homologa.php? Id_insumo = $ row 2 [id_insumo])> $ row2 [branch] </ A> </ font> </ td> ";
)
else
(
$ s1 = "id_insumo = $ row2 [id_insumo] & input = $ row2 [des_insumo]";
print "<td> <font face=arial size=3> <A HREF = JavaScript & #058; popUp (homologa.php? id_insumo = $ row 2 [id_insumo])> Index </ A> </ font> </ td> ";
$ homologa_completo = 1;
)
print "</ tr>";
)
mysql_free_result ($ result2);

print "</ table>";
if ($ homologa_completo == 0)
(
print "To continue the monthly adjustment adjustment selections NEWS </ b>";
$ result = mysql_query ( "update tb_contrato September homologa_completo =" 1 "where id_contrato = $ id_contrato;");

)
break;
<br > case 10:
/ / PAGE SETUP
imprime_ajuste_mensual_1 ();
break;

add a comment
0