Aaron Warnke
Aaron Warnke

Reputation: 213

How to set radio button in php?

Hi I am trying to get the value of whatever radio button is set to and pass that to an array or switch, whatever will work, to set the background color. The following code works and updates the database only if the default radio button is set, how can I set the 'background_color' like I did with default below, like this default =1, blue =2, red =3, orange =4, and green =5.

<?php
if(isset($_GET['success']) === true && empty($_GET['success']) === true){
echo 'Background color changed.';
}else{



    if(isset($_POST['color']) && $_POST['color'] == 'default'){
    $update_data = array(
        'background_color'  =>  1
        );


    update_user($session_user_id, $update_data);
    header('Location: edit_background.php?success');
    exit();

}else if(empty($errors) === false){
    echo output_errors($errors);
}
?>

<form action="" method="post">
<input type="radio" name="color" value="default" <?php if($user_data['background_color'] ==1){echo 'checked="checked"';}?>/> Default<br />
<input type="radio" name="color" value="blue" <?php if($user_data['background_color'] ==2){echo 'checked="checked"';}?>/> Blue<br />
<input type="radio" name="color" value="red" <?php if($user_data['background_color'] ==3){echo 'checked="checked"';}?>/> Red<br />
<input type="radio" name="color" value="orange" <?php if($user_data['background_color'] ==4){echo 'checked="checked"';}?>/> Orange<br />
<input type="radio" name="color" value="green" <?php if($user_data['background_color'] ==5){echo 'checked="checked"';}?>/> Green<br />
<input type="submit" value="Submit">
</form>

Upvotes: 1

Views: 5999

Answers (2)

John C
John C

Reputation: 8415

The code you've posted is missing a curly brace somewhere, but ignoring that I think your problem is translating between the IDs for the colours and the names. I've made a reworked version of your code that takes advantage of arrays to manage the data:

<?php

// Define backgrounds as an array 
$backgrounds['default'] = 1;
$backgrounds['blue'] = 2;
$backgrounds['red'] = 3;
$backgrounds['orange'] = 4;
$backgrounds['green'] = 5;

// If a form has been posted with a valid colour
if(isset($_POST['color']) && array_key_exists($_POST['color'], $backgrounds)){

    $update_data = array(
        'background_color'  =>  $backgrounds[$_POST['color']]
    );

    update_user($session_user_id, $update_data);
    echo 'Background color changed.';

}

// Output the form
echo '<form action="" method="post">';

// Loop through backgrounds and display radio buttons
foreach ($backgrounds as $bgname => $bgid) {
    echo '<input type="radio" name="color" value="'.$bgname.'"';
    if ($user_data['background_color'] == $bgid){
        echo 'checked="checked"';
    }
    echo '/> '.$bgname.'<br />';
}

echo '<input type="submit" value="Submit">
</form>';
?>

Upvotes: 2

Aaron Warnke
Aaron Warnke

Reputation: 213

I figured it out and wanted to answer so anyone else looking to figure this out can see, this is what I had to do: Change:

if(isset($_POST['color']) && $_POST['color'] == 'default'){
$update_data = array(
    'background_color'  =>  1
    );

To:

if(isset($_POST['submit'])){

    $selected_radio = $_POST['color'];

    if($selected_radio == 'default'){
        $update_data['background_color'] = 1;
    }else if($selected_radio == 'blue'){
        $update_data['background_color'] = 2;
    }else if($selected_radio == 'red'){
        $update_data['background_color'] = 3;
    }else if($selected_radio == 'orange'){
        $update_data['background_color'] = 4;
    }else if($selected_radio == 'green'){
        $update_data['background_color'] = 5;
    }

Upvotes: 1

Related Questions