开发者

Easiest Way to send Checkbox form over PHP

So I have a lot of checkboxes that need to be passed to a PHP script, and then for every checkbox that is checked, a value is written to a file.

For example, if 7:30 - 8:30 is checked, then the PHP fwrites to a file named times. That much I can handle. The part where I am having the trouble is passing the checkboxes to the PHP. Is this the smartest way to go about it?

<div id="table">
<form action="updatetime.php" method="post">
<table>
<tr><td>
<table>
<tr><td>8:00 AM - 8:30 AM</td><td><input type="checkbox" name="8830AM" value="9:30 PM - 10:00 PM" /></td></tr>
<tr><td>8:30 AM - 9:00 AM</td><td><input type="checkbox" name="8309AM" value="8:30 AM - 9:00 AM" /></td></tr>
<tr><td>9:00 AM - 9:30 AM</td><td><input type="checkbox" name="9930AM" value="9:00 AM - 9:30 AM开发者_开发技巧" /></td></tr>
<tr><td>9:30 AM - 10:00 AM</td><td><input type="checkbox" name="93010AM" value="9:30 AM - 10:00 AM" /></td></tr>
<tr><td>10:00 AM - 10:30 AM</td><td><input type="checkbox" name="101030AM" value="10:00 AM - 10:30 AM" /></td></tr>
<tr><td>10:30 AM - 11:00 AM</td><td><input type="checkbox" name="103011AM" value="10:30 AM - 11:00 AM" /></td></tr>
<tr><td>11:00 AM - 11:30 PM</td><td><input type="checkbox" name="111130AM" value="11:00 AM - 11:30 PM" /></td></tr>
<tr><td>11:30 AM - 12:00 PM</td><td><input type="checkbox" name="113012PM" value="11:30 AM - 12:00 PM" /></td></tr>
<tr><td>12:00 PM - 12:30 PM</td><td><input type="checkbox" name="121230PM" value="12:00 PM - 12:30 PM" /></td></tr>
<tr><td>12:30 PM - 1:00 PM</td><td><input type="checkbox" name="12301PM" value="12:30 PM - 1:00 PM" /></td></tr>
<tr><td>1:00 PM - 1:30 PM</td><td><input type="checkbox" name="1130PM" value="1:00 PM - 1:30 PM" /></td></tr>
<tr><td>1:30 PM - 2:00 PM</td><td><input type="checkbox" name="1302PM" value="1:30 PM - 2:00 PM" /></td></tr>
<tr><td>2:00 PM - 2:30 PM</td><td><input type="checkbox" name="2230PM" value="2:00 PM - 2:30 PM" /></td></tr>
<tr><td>2:30 PM - 3:00 PM</td><td><input type="checkbox" name="2303PM" value="2:30 PM - 3:00 PM" /></td></tr></table></td><td><table>
<tr><td>3:00 PM - 3:30 PM</td><td><input type="checkbox" name="3330PM" value="3:00 PM - 3:30 PM" /></td></tr>
<tr><td>3:30 PM - 4:00 PM</td><td><input type="checkbox" name="3304PM" value="3:30 PM - 4:00 PM" /></td></tr>
<tr><td>4:00 PM - 4:30 PM</td><td><input type="checkbox" name="4430PM" value="4:00 PM - 4:30 PM" /></td></tr>
<tr><td>4:30 PM - 5:00 PM</td><td><input type="checkbox" name="4305PM" value="4:30 PM - 5:00 PM" /></td></tr>
<tr><td>5:00 PM - 5:30 PM</td><td><input type="checkbox" name="5530PM" value="5:00 PM - 5:30 PM" /></td></tr>
<tr><td>5:30 PM - 6:00 PM</td><td><input type="checkbox" name="5306PM" value="5:30 PM - 6:00 PM" /></td></tr>
<tr><td>6:00 PM - 6:30 PM</td><td><input type="checkbox" name="6630PM" value="6:00 PM - 6:30 PM" /></td></tr>
<tr><td>6:30 PM - 7:00 PM</td><td><input type="checkbox" name="6307PM" value="6:30 PM - 7:00 PM" /></td></tr>
<tr><td>7:00 PM - 7:30 PM</td><td><input type="checkbox" name="7730PM" value="7:00 PM - 7:30 PM" /></td></tr>
<tr><td>7:30 PM - 8:00 PM</td><td><input type="checkbox" name="7308PM" value="7:30 PM - 8:00 PM" /></td></tr>
<tr><td>8:00 PM - 8:30 PM</td><td><input type="checkbox" name="8830PM" value="8:00 PM - 8:30 PM" /></td></tr>
<tr><td>8:30 PM - 9:00 PM</td><td><input type="checkbox" name="8309PM" value="8:30 PM - 9:00 PM" /></td></tr>
<tr><td>9:00 PM - 9:30 PM</td><td><input type="checkbox" name="9930PM" value="9:00 PM - 9:30 PM" /></td></tr>
<tr><td>9:30 PM - 10:00 PM</td><td><input type="checkbox" name="93010PM" value="9:30 PM - 10:00 PM" /></td></tr>

</table>
</tr></td>
</table>
<div style="margin-right:auto;margin-left:auto;width:40px;padding-right:30px !important">
<input type="submit" name="submit" value="Update Times" />
</div>
</form>

(Don't hate on me for my table usage, this IS a table after all.)

So the way I have it set up I would get the value of each by doing something along the lines of

$a = $_POST['8830AM'];
$b = $_POST['8309AM'];

... and so on and so forth, then

$data = $a . '\n' . $b . '\n' . ...
$handle = fopen('times', 'w') or die('Failure.');
fwrite($handle, $data);
fclose($handle);

I'm sure there is an easier way to do this using a loop or something. I can change the table to my hearts content. Help me out!

Thanks!


Yes there is:

<tr><td>8:30 PM - 9:00 PM</td><td><input type="checkbox" name="timeframes[]" value="8:30 PM - 9:00 PM" /></td></tr>

...

if (isset($_POST['timeframes'])) {
    foreach ($_POST['timeframes'] as $timeframe) {
        ...
    }
}


Just give them all the same name: <input name="time[]" />

And then read it as an array with $_POST['time']


if you use bracket notation in the name like name="checkboxgroup[8830AM]", the variable $_POST['checkboxgroup'] will then be an array, it's that simple :)


the $_POST array is an associative key=>value array, and in this case the key will be the name of the checked box (ex. 903010PM) and the value will be whatever is in the input's 'value' attribute (ex. 9:30 PM - 10:00 PM)

if(isset($_POST)){

    foreach($_POST as $key => $val){
        $key = ... (ex. 903010PM)
        $val = ... (ex. 9:30 PM - 10:00 PM)
    }

}

So if (10:00 AM - 10:30 AM), (11:00 AM - 11:30 AM), and (12:00 PM - 12:30 PM) were selected, the following code:

if(isset($_POST)){
    foreach($_POST as $key => $val){
        echo "key: ($key)  value: ($val)  <br/>";
    }
}

would output:

key: (101030AM) value: (10:00 AM - 10:30 AM)

key: (111130AM) value: (11:00 AM - 11:30 AM)

key: (121230PM) value: (12:00 PM - 12:30 PM)


I have tried an easy way to create checkboxes as well as radio buttons in any php form. Only thing is I am using Codeigniter MVC framework.

Here is the function definition that you can insert in your common-model or any helper file.

function createOptions($fieldName, $labelsArray=array(), $selectedOption, $fieldType,$valuesArray = array()) {
        $returnString = '';
        if(count($valuesArray)!=count($labelsArray))
            $valuesArray=$lebelsArray;
        if ($fieldType === 'checkbox') {
            for ($i=0;$i<count($labelsArray);$i++) {
                $returnString.='&nbsp&nbsp&nbsp<input type="checkbox" name=' . $fieldName.' value='.$valuesArray[$i].' id='.$valuesArray[$i];
                if(in_array($valuesArray[$i], $selectedOption)){
                        $returnString.=' checked="checked" ';
                }
                $returnString.=' />&nbsp&nbsp<label>'.$labelsArray[$i].'</label>';
            }
        }
        if ($fieldType === 'radio') {
            for ($i=0;$i<count($labelsArray);$i++) {
                $returnString.='&nbsp&nbsp<input type="radio" name=' . $fieldName.' value='.$valuesArray[$i].' id='.$valuesArray[$i];
                if($valuesArray[$i]== $selectedOption)
                        $returnString.=' checked="checked" ';
                $returnString.=' /><label>'.$labelsArray[$i].'</label>';
            }
        }
        return $returnString;
    }

And, you have to call this function in view file as,

<?php
echo $this->common_model->createOptions('userHobbies[]', $hobbyOptions, $userHobbies, 'checkbox'); ?>

First parameter is name of checkbox field or radio field, which is always gonna be same for all options for both cases. Second is labels array, Third is selected options which will show those options as checked while loading the form. Fourth is type of field that will be a string as 'checkbox' or 'radio'. Fifth will be values array, which, if present, will contain values for labels in the same order as that of labels. If its absent, labels array will be treated as values array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜