如何获取单选按钮的状态

时间:2010-12-19 17:08:19

标签: php

请问如何根据数据库中的值来获取单选按钮的状态? 我想根据数据库中各自的值将它们设置为已选中或未选中。 field_type是int(1或0)。 有没有更有效的方法来做到这一点?

$ news_alert_status_a =“unchecked”;         $ news_alert_status_k =“unchecked”;         $ news_alert_status_n =“unchecked”;

    $events_alert_status_a = "unchecked";
    $events_alert_status_k = "unchecked";
    $events_alert_status_n = "unchecked";

    $questions_alert_status_a = "unchecked";
    $questions_alert_status_k = "unchecked";
    $questions_alert_status_n = "unchecked";

    $editorials_alert_status_a = "unchecked";
    $editorials_alert_status_k = "unchecked";
    $editorials_alert_status_n = "unchecked";


    if ($news_alert == 1)
    {
        $news_alert_status_a = "checked";
    }
    elseif ($news_alert == 2)
    {
        $news_alert_status_k = "checked";
}
elseif($news_alert == 3)
{
    $news_alert_status_n = "checked";
}


if ($events_alert == 1)
    {
        $events_alert_status_a = "checked";
    }
    elseif ($events_alert == 2)
    {
        $events_alert_status_k = "checked";
}
elseif($events_alert == 3)
{
    $events_alert_status_n = "checked";
}

if ($questions_alert == 1)
    {
        $questions_alert_status_a = "checked";
    }
    elseif ($questions_alert == 2)
    {
        $questions_alert_status_k = "checked";
}
elseif($questions_alert == 3)
{
    $questions_alert_status_n = "checked";
}       

if ($editorials_alert == 1)
    {
        $editorials_alert_status_a = "checked";
    }
    elseif ($editorials_alert == 2)
    {
        $editorials_alert_status_k = "checked";
}
elseif($editorials_alert == 3)
{
    $editorials_alert_status_n = "checked";
}           

2 个答案:

答案 0 :(得分:0)

The PHP code:

<?PHP

$male_status = 'unchecked';
$female_status = 'unchecked';

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

$selected_radio = $_POST['gender'];

if ($selected_radio = = 'male') {
$male_status = 'checked';
}
else if ($selected_radio = = 'female') {
$female_status = 'checked';
}
}

?>

The HTML FORM code:

<FORM name ="form1" method ="post" action ="radioButton.php">

<Input type = 'Radio' Name ='gender' value= 'male'
<?PHP print $male_status; ?>
>Male

<Input type = 'Radio' Name ='gender' value= 'female'
<?PHP print $female_status; ?>
>Female

<P>
<Input type = "Submit" Name = "Submit1" VALUE = "Select a Radio Button">
</FORM>

答案 1 :(得分:0)

<?php
if($_POST)
{
 if($_POST['gender'] != "")
 {
  $gender = $_POST['gender'] == "male" ? 1 : 0;
 }
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Radio Button</title>
</head>

<body>
<form method="post">
<label for="radio_female">Female</label><input type="radio" name="gender" id="radio_female" value="female"/>
<label for="radio_male">Male</label><input type="radio" name="gender" id="radio_male" value="male"/>
<input type="submit" value="Submit" />
</form>
</body>
</html>