Make array from checkbox form

后端 未结 8 1786
囚心锁ツ
囚心锁ツ 2020-12-09 11:42

My friend and I are making a website that compiles news stories based on your interests. Is there and easy way to take the checkbox data and make an array out of the selecte

相关标签:
8条回答
  • 2020-12-09 12:15

    Sorry, posted before I was done writing:(

    Just a few improvements to the suggestions already posted:

    Use labels for the form:

    <label for="check_politics">Politics</label>
    <input type="checkbox" name="intrests[]" id="check_politics" value="Politics"/>
    

    Using labels to enhance a form is brilliant in my opinion:) Set their display to block if you want them to get linebreaks.

    And use foreach to loop through it on the serverside:

    $intrests = $_POST['intrests'];
    foreach($intrests as $intrest) {
        echo $intrest . " is my intrest";
    }
    
    0 讨论(0)
  • 2020-12-09 12:16

    hey, I have made it easy 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 teated as values array.

    0 讨论(0)
提交回复
热议问题