AKor
AKor

Reputation: 8882

Pulling form data from key-value pairs

I'm trying to work with the data coming in from a form (full of checkboxes, if that matters).

The output of the POST looks like "buzz: on, asdf: on, userid: 1".

if(isset($_POST['userid']))
{
    $postedUserID = $_POST['userid'];
    foreach ($_POST as $key => $value)
    {
        switch($key)
        {
            case "buzz":
                $old_services = get_user_meta($postedUserID, 'whatservices', true);
                $updated_services = $old_services . " buzz";
                update_user_meta($postedUserID, 'whatservices', $updated_services);
                break;
            default:
                echo "Something is wrong.";
        }
    }
}

I think I'm just confusing myself a bit with the switch and with the key value pairs. How can I have the switch actuate when $_['buzz'] = on?

(The reason I'm using a switch is because this will eventually have a LOT of conditions, >20.)

Upvotes: 0

Views: 83

Answers (1)

u_mulder
u_mulder

Reputation: 54841

Maybe you need this:

foreach ($_POST as $key => $value)
{    
    switch($key)
    {
        case "buzz":
            if ($value == 'on') {
                // do something
            }
            break;
        // more cases
     }
}

Upvotes: 2

Related Questions