Nazly
Nazly

Reputation: 65

Retrieving data from checkbox - php

I have a Choice list contain 4 types of Articles. The user should take one choice. Now the list become contain 45 Articles and I changed the choice list by a checkBox listfor multiple choices.

This following the old setter function:

 public function setNature($nature) {
        $this->_nature = $nature;
        if ($nature === 'Production') { $this->_nature='Production';   }
        elseif ($nature === 'A'){ $this->_nature='A';}
        elseif ($nature === 'B') {$this->_nature='B';}
        else $this->_nature ='all';
    }

How I can change this setter function to recovre the data without write all the 45 types of articles ?

Upvotes: 1

Views: 42

Answers (2)

Krunal Limbad
Krunal Limbad

Reputation: 1573

<form method='post' id='userform' action='thisform.php'> <tr>
    <td>Trouble Type</td>
    <td>
    <input type='checkbox' name='checkboxvar[]' value='Option One'>1<br>
    <input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br>
    <input type='checkbox' name='checkboxvar[]' value='Option Three'>3
    </td> </tr> </table> <input type='submit' class='buttons'> </form>

<?php 
if (isset($_POST['checkboxvar'])) 
{
    print_r($_POST['checkboxvar']); 
}
?>

Hope this Helps

echo implode(',', $_POST['checkboxvar']); // change the comma to whatever separator you want

Upvotes: 0

Florian Humblot
Florian Humblot

Reputation: 955

You could automate the looking for allowed natures like so:

public function setNature($nature) {
    $allowed_nature = ['A','B','Production'];
    if(in_array($nature, $allowed_nature)){
        $this->_nature = $nature;
    }else{
        $this->_nature = 'all';
    }
}

The only negative side is that you need to store the allowed natures somewhere, in this case that would be an array, but it could be from your database as well.

Based on your current information, this is what I can make of it!

Upvotes: 1

Related Questions