Link to home
Start Free TrialLog in
Avatar of ian646
ian646

asked on

checkboxes

Hi
Help! A beginner having real problems getting my head around checkboxes.  None of my books
deal with checkboxes and the web has given no clear info on this!  I have produced a simple
form which needs checkboxes.
It is needed to log student timetable information (AM/PM sessions).  A user will
tick some/all of the checkboxes which will then be inserted into the relevant column in my mysql table.
i will need to diplay results in HTML  and the user updates the checkboxes if necessary.
thanks for your help on this, it is driving me nuts.
Avatar of AriMc
AriMc
Flag of Finland image

It's probably just a confusion about the HTML side of checkboxes. Basically you insert them in your PHP-script with code like this:

echo "<LABEL>Use option X: </LABEL>";
echo "<INPUT type=\"checkbox\" name=\"optionx\" value=\"1\"";

if ($optionx==1)
   echo " CHECKED=\"YES\"";

echo ">";

Here the variable $optionx contains zero or one depending on whether you want the checkbox to be initially checked or not.

Then in your receiving PHP-script you can determine the checkbox status like this:

$optionx = $_GET['optionx'];

This little script will help you understand them.  Install it and run it to see the moving parts.  Checkboxes that are not checked do not appear in the action script at all (unlike empty input type=text controls).
<?php // RAY_checkbox_test.php
error_reporting(E_ALL);

// IF THE FIELDS HAVE BEEN POSTED
if (!empty($_POST["_submit"]))
{
// CHECK FOR AT LEAST ONE BOX CHECKED - REMOTE THE SUBMIT BUTTON
   unset($_POST["_submit"]);

// IF THE POST ARRAY IS EMPTY NOW, NO BUTTONS WERE CHECKED
   if (empty($_POST))
   {
      echo "YOU DID NOT CHOOSE A CHECKBOX";
   }
   else
   {
      echo "THANK YOU!";
      var_dump($_POST); // SHOW WHAT WAS CHECKED
   }
}
// END OF PHP - PUT UP FORM
?>
<html>
<body>
<form method="POST">
CHECK OR UNCHECK BOXES<br/>
BOX1 <input name="box1" type="checkbox" value="MyBox1" <?php if (isset($_POST["box1"])) echo " checked"; ?> /><br/>
BOX2 <input name="box2" type="checkbox" value="MyBox2" <?php if (isset($_POST["box2"])) echo " checked"; ?> /><br/>
<input type="submit" name="_submit"  value="go" />
</form>
</body>
</html>

Open in new window

Another example that might be worth a look...
<?php // RAY_checkbox_array.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

// DEMONSTRATE HOW TO SUBMIT CHECKBOXES IN THE FORM OF ARRAYS
// NOTE THAT THE UNCHECKED CHECKBOXES DO NOT COME THROUGH INTO $_POST
// NOTE THAT THE UNNAMED INPUT TAGS DO NOT COME THROUGH INTO $_POST

if (!empty($_POST["godo"]))
{
    // WHICH SUBMIT BUTTON FIRED?
    echo "YOU SUBMITTED {$_POST["godo"]}";
    echo PHP_EOL;
    var_dump($_POST);
}
// END OF PHP - USE HTML AND JAVASCRIPT TO PUT UP THE FORM
?>

<script language="JavaScript">
function toggle(formName, arrayName, YesNo)
{
   var cbox=document.getElementsByName(formName)[0].getElementsByTagName('input');
   for(i=0;i<cbox.length;i++)
   {
      if (cbox[i].name.substr(0,arrayName.length) == arrayName) cbox[i].checked=YesNo;
   }
}
</script>

<form method="post" name="checkForm">
GROUP A - SINGLE CLICK OR SELECT / DESELECT ALL
<input name="abox[1]" type="checkbox" value="A1" /> A1
<input name="abox[X]" type="checkbox" value="AX" /> AX
<input name="abox[3]" type="checkbox" value="A3" /> A3
<input                type="checkbox" onClick="toggle('checkForm', 'abox', this.checked)"> ALL OF 'A' (NO REFERENCE TO ANY OF 'B')

GROUP B - SINGLE CLICK OR SELECT / DESELECT ALL
<input name="bbox[1]" type="checkbox" /> B1
<input name="bbox[2]" type="checkbox" /> B2
<input name="allofB"  type="checkbox" onClick="toggle('checkForm', 'bbox', this.checked)"> ALL OF 'B' (NO REFERENCE TO ANY OF 'A')

GROUP C - SINGLE CLICK ONLY
<input name="cbox[]" type="checkbox" value="One" /> C[]
<input name="cbox[]" type="checkbox" value="Two" /> C[]
<input name="cbox[]" type="checkbox" value="Tre" /> C[]

SOME ADDITIONAL FORM DATA
<input name="thing"   type="text"    value="FOO" />
<input                type="text"    value="BAR" />
<input name="godo"    type="submit"  value="GO" />
<input name="godo"    type="submit"  value="DO" />
</form>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ian646
ian646

ASKER

thank you very much for the feedback.  i will work through your code and let u know how i got on. thanks again
I may be away for a while, but I check EE daily.  Try the examples and see what you learn, then post back and I will be glad to help.  Best of luck! ~Ray
Avatar of ian646

ASKER

Hi Ray
i have tried yr first code and it worked.  i imagine instead of var_dump($_POST) i need to use an echo command to display what was checked in HTML?  Also, how would i get those values into my sql database?
thanks
Ian
echo, var-dump, and print_r are all ways of visualizing data.  As far as getting values into a data base, I want to ask you to please consider buying that book and reading it.  It teaches things that go in depth beyond what we can do here at EE with a question like that.  The difficulty of answering such a broad and open-ended question is that we do not know what you want to do with the data base, and so our advice is likely to be wrong, or at best ignorant of your particular applications' needs.

http://www.sitepoint.com/books/phpmysql4/

All the best, ~Ray
Avatar of ian646

ASKER

thanks Ray - think i'll buy the book! easy learning text fields etc but books seem to subtley avoid the question of checkboxes and how they manage data.  most websites i visit either avoid the subject or seem to be as confused as i am!
thanks for your help.
ian
Avatar of ian646

ASKER

Ray,
I have got the checkboxes working and choices made will now display in HTML as verification to the user  - can you point me in how i save this checkbox data into multiple columns in my dbase?  i can show some of my code if necessary?! also, does this book specifically focus on checkboxes and linkage to sql databses?  I would be mightily disppaointed otherwise!
thanks
The book teaches just what its title says.  Checkboxes are about 1/1000 of what you need to know to use HTML, PHP and data bases together.

In the REQUEST array (GET or POST) the value associated with a checked checkbox is present.  The "name" from the HTML form is the index of the $_POST or $_GET array, and the "value" from the form is the value of that position in the array.  With no value given, I believe that most browsers will send "on" and if there is no checkbox with that name checked the array element is not set.

Do you know how to connect, select and update a data base table?  If not, you might want to post a new question about that part of the application.
Avatar of ian646

ASKER

Hi
i got the book and am slowly working my way through it.  i have also bought 2 other books - i seem to have found it difficult to find  a chapter (and examples) which will take a checkbox array and insert the checked data into a sql table!?!
thanks for your help anyway  -  
Let me try this one again: Do you know how to connect, select and update a data base table?
Avatar of ian646

ASKER

yes  - i am fine with text boxes and radio buttons and have those working in my form.  i have little info on checkboxes  - my books tend to pass checkboxes by!  tried last night using the implode function to post into a column in the table but i couldn't get it to work.  the field remained blank.  i am amazed on the scarcity of info in beginners books. may ask a php expert to finish my project.
sorry for being so dense Ray - i appreciate your help.
Buy that SitePoint book.  Give yourself a month or two to work through the examples.  You won't be "dense" at all after you see the instructions and examples that SitePoint provides.  But don't expect instant gratification - programming does not work that way at all.  This explains why.
http://norvig.com/21-days.html

The script I posted at ID:34912332 is available on my server at http://www.laprbass.com/RAY_checkboxes.php -- if you read the code and try experimenting with different checkboxes there you will be able to see how they work.

Here is another learning resource: http://us3.php.net/tut.php

The problem you face (and we all face as we learn web technologies) is that there is not one subject that can be mastered first.  You have to learn HTML, CSS, PHP, MySQL, principles of user-experience design, JavaScript, jQuery, and web services all at the same time.  To compound that problem, HTML and CSS are designed to fail silently.  You will have hateful little typographical errors and you won't be able to find them.  It can all be very frustrating, and I sympathize with your plight.

Best of luck, ~Ray
Oops, sorry - I see you already got the book.  Great Start!  Charge ahead and use EE every time you come to something that gives you pause.  There are lots of experts here who know PHP very well and are eager to help.

Cheers!
The response at ID:34912332 covers it, I think.
Avatar of Guy Hengel [angelIII / a3]
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.