如何在PHP中设置单选按钮?

时间:2012-08-03 01:46:21

标签: php html

您好我正在尝试获取设置的任何单选按钮的值,并将其传递给数组或开关,无论什么工作,设置背景颜色。以下代码仅在设置了默认单选按钮时才能工作和更新数据库,如何设置'background_color',就像我在下面默认设置的那样,例如default = 1,blue = 2,red = 3,orange = 4,和绿色= 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>

2 个答案:

答案 0 :(得分:2)

您发布的代码在某处缺少大括号,但忽略了我认为您的问题是在颜色和名称的ID之间进行转换。我已经制作了一个重新编写的代码版本,它利用数组来管理数据:

<?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>';
?>

答案 1 :(得分:1)

我想通了,想要回答,所以任何想要弄明白的人都可以看到,这就是我必须做的事情: 变化:

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

要:

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;
    }