DESPERATE- gettin checkbox values in POST (PHP)

  • JackDaRippaZ
  • Newbie
  • Newbie
  • JackDaRippaZ
  • Posts: 11

Post 3+ Months Ago

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

Code: [ Select ]
<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
  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

Quote:
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:

Code: [ Select ]
<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
  1. <input type="checkbox" name="fruit" value="apples">apples
  2. <input type="checkbox" name="fruit" value="oranges">oranges
  3. <input type="checkbox" name="fruit" value="peaches">peaches
  4. <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.
  • JackDaRippaZ
  • Newbie
  • Newbie
  • JackDaRippaZ
  • Posts: 11

Post 3+ Months Ago

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
  • Cafu
  • Student
  • Student
  • Cafu
  • Posts: 97

Post 3+ Months Ago

will this help?


Code: [ Select ]
<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>
  1. <html>
  2. <head>
  3. <title>checkbox help</title>
  4. </head>
  5. <?
  6. if (isset($HTTP_POST_VARS)) {
  7.     $fruit = $HTTP_POST_VARS["fruit"];
  8.     echo("Fruits chosen: " . count($fruit) . "<br><br>");
  9.     if (count($fruit)>0) {
  10.         echo("You chose the following fruits:<br>");
  11.     }
  12.     for ($i=0; $i<count($fruit); $i++) {
  13.         echo( ($i+1) . ") " . $fruit[$i] . "<br>");
  14.     }
  15. }
  16. ?>
  17. <body bgcolor="#ffffff">
  18. <form method="post">
  19. Choose a fruit:<br><br>
  20. <input type="checkbox" name="fruit[]" value="apples">apples <br>
  21. <input type="checkbox" name="fruit[]" value="oranges">oranges <br>
  22. <input type="checkbox" name="fruit[]" value="peaches">peaches <br>
  23. <input type="checkbox" name="fruit[]" value="mangos">mangos<br>
  24. <input type="submit">
  25. </form>
  26. </body>
  27. <html>
  • drdrano
  • Born
  • Born
  • drdrano
  • Posts: 1
  • Loc: La Crosse, WI, USA

Post 3+ Months Ago

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.

Code: [ Select ]
<!-- 
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>
  1. <!-- 
  2. Instructive notes:
  3.     1. to use the   if (isset($_POST['submit']))  construct, the submit button must have a name defined as 'submit'
  4.     2. if each of the checkboxes has been named with the same name (ie 'fruit') followed by a pair of square brackets '[]',
  5. when the form is submitted, the $_POST superglobal will have a variable named $_POST['fruit'] that represents an
  6. array containing array elements from only those checkboxes that have been checked.
  7.     3. count() returns the number of elements in an array. here it returns '0' if no checkboxes are checked. This could be 
  8. because $_POST['fruit'] is not set, or because $_POST['fruit'] represents an array that has been set but has no elements.
  9. if we change the if statement to  if (isset($_POST['fruit'])), the statements in the 'if' block are never executed, 
  10. so $_POST['fruit'] is not set and must evaluate to null. count() accurately returns '0' when its parameter is null.
  11.     4. the value of each element in the array $_POST['fruit'] is determined by the value attribute of each of the checkboxes.
  12.     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.
  13. -->
  14. <html>
  15. <head>
  16. <title>checkbox help</title>
  17. </head>
  18. <?php
  19. if (isset($_POST['submit'])) {
  20.     $fruit = $_POST["fruit"];
  21.     $how_many = count($fruit);
  22.     echo 'Fruits chosen: '.$how_many.'<br><br>';
  23.     if ($how_many>0) {
  24.         echo 'You chose the following fruits:<br>';
  25.     }
  26.     for ($i=0; $i<$how_many; $i++) {
  27.         echo ($i+1) . '- ' . $fruit[$i] . '<br>';
  28.     }
  29.         echo "<br><br>";
  30. }
  31. ?>
  32. <body bgcolor="#ffffff">
  33. <form method="post">
  34. Choose a fruit:<br><br>
  35. <input type="checkbox" name="fruit[]" value="apples">apples <br>
  36. <input type="checkbox" name="fruit[]" value="oranges">oranges <br>
  37. <input type="checkbox" name="fruit[]" value="peaches">peaches <br>
  38. <input type="checkbox" name="fruit[]" value="mangos">mangos<br>
  39. <input type="submit" name = "submit">
  40. </form>
  41. </body>
  42. <html>


Larry Fundell
  • trekie8472
  • Born
  • Born
  • trekie8472
  • Posts: 1
  • Loc: Minneapolis, MN

Post 3+ Months Ago

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.
  • murcielagossi
  • Proficient
  • Proficient
  • User avatar
  • Posts: 457

Post 3+ Months Ago

Or you could use "foreach":

PHP Code: [ Select ]
 
foreach($_POST['check']  as  $value)  {
 
$check_msg .= "Checked: $value\n";
 
}
 
 
  1.  
  2. foreach($_POST['check']  as  $value)  {
  3.  
  4. $check_msg .= "Checked: $value\n";
  5.  
  6. }
  7.  
  8.  


The accompanying form code would be:

Code: [ Select ]
<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>
  1. <input type="checkbox" name="check[]" value="blue_color"> Blue<br>
  2. <input type="checkbox" name="check[]" value="green_color"> Green<br>
  3. <input type="checkbox" name="check[]" value="orange_color"> Orange<br>


It that doesn't clarify it enough --

http://www.kirupa.com/web/php_contact_form3.htm
  • flann
  • Novice
  • Novice
  • flann
  • Posts: 25
  • Loc: Wisconsin

Post 3+ Months Ago

is there anyway to get all the checkboxes through a post, even the ones that aren't checked? I too am from La Crosse :).
  • ydajdaj
  • Born
  • Born
  • ydajdaj
  • Posts: 1

Post 3+ Months Ago

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;

/ / PAGE SETUP
imprime_ajuste_mensual_1 ();
break;

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 28 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.