5 replies
Can anyone tell me how to use check boxes in the following php files?

contact form - contact.php
template for mail - contact-mail-html.tpl.php
contact-process.php - validates form
#boxes #check #php
  • Profile picture of the author heroic
    Since you have not provided the code for these files, i'll let u know the basic behind checkboxes in PHP

    say ur html goes as <input type="checkbox" name="my_checkBox" value="myValue" />

    then in PHP
    <?=(isset($_PHP['my_checkBox']))?'checkbox was checked':'It was not checked'; ?>
    {{ DiscussionBoard.errors[2370221].message }}
  • Profile picture of the author luminousnerd
    It depends what you want to do? PHP doesn't actually output check boxes, that's done with HTML. PHP is for using the data it gets from that checkbox, either to change what the user sees, put the info in a database, or something like that.

    A checkbox is as simple as

    <input type="checkbox">

    Though you'll probably want a name and a value as well to do anything useful with it.
    {{ DiscussionBoard.errors[2371859].message }}
  • Profile picture of the author godwinsam

    Below I am mentioned the code for check boxes in php with form action. Please find it.


    while (list ($key,$val) = @each ($box)) {
    echo "$val,";

    echo "<form method=post action=''>";
    echo "<table border='0' cellspacing='0' style='border-collapse: collapse' width='100' >
    <tr bgcolor='#ffffff'>
    <td width='25%'><input type=checkbox name=box[] value='John'></td>
    <td width='25%'>John</td>
    <td width='25%'><input type=checkbox name=box[] value='Mike'></td>
    <td width='25%'>Mike</td>
    <td width='25%'><input type=checkbox name=box[] value='Rone'></td>
    <td width='25%'>Rone</td>
    <tr><td colspan =6 align=center><input type=submit value=Select></form></td></tr>
    {{ DiscussionBoard.errors[2372708].message }}
  • Profile picture of the author Itamar
    you need to have the checkbox added in the main file (the form)

    and then in PHP the only thing you really need to use is
    the isset() command , since a checkbox in PHP will never be available in the $_POST array
    unless it's checked.

    If it's checked , it will appear as TRUE , or 1. so the only real thing left to do when working it out on the php side
    is to check if it's SET with isset($_POST["mycheckbox"])
    {{ DiscussionBoard.errors[2385321].message }}
    • Profile picture of the author WarriorForSuccess
      It won't equal TRUE or 1... It'll equal the value. i.e. John, Mike, Rone

      isset is appropriate; just make sure you know what you're getting as the value. This is a checkbox array. It's not as straight-forward. You'll get an array called "box" if ANY of the checkboxes are set, and then you have to iterate (typically using "foreach") to find all the checkboxes checked.

      PM me if there are further questions. I do cover this in my course at OnlinePHPClass.com, and if you are interested, click the WSO link in my signature.
      {{ DiscussionBoard.errors[2513683].message }}

Trending Topics