1

嗨,我正在尝试获取设置为任何单选按钮的值并将其传递给数组或开关,无论是否可行,以设置背景颜色。以下代码仅在设置默认单选按钮时才有效并更新数据库,我如何设置'background_color',就像我在下面使用默认设置一样,就像这个默认=1,蓝色=2,红色=3,橙色=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>
4

2 回答 2

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>';
?>
于 2012-08-03T02:49:24.300 回答
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;
    }
于 2012-08-03T02:47:16.090 回答