如果未选中一个单选按钮,则显示错误?

时间:2013-03-11 10:22:09

标签: php html mysql

如果我问mysql_ *格式,我很抱歉,因为它正是我现在正在使用的。我仍然希望你们能帮助我。

所以我的问题是,我正在尝试在提交表单时显示错误,其中未选中一行单选按钮。但是,基于我的代码,它只出现在特定的行上而不是作为单个消息在顶部,因为我认为我已将它包含在While-Loop中。你们可以就如何做到这一点提出建议吗?无论是javascript还是函数,我都很好。我只是不知道该怎么做:P。

这是我的代码:

while($fetch=mysql_fetch_array($query)){

//DISPLAY ALL ANSWERS
if(isset($_POST['voted'])){
    if(in_array($_POST['score('.$fetch['questionID'].')'], $radioValue)){
        echo "<tr>Question no. ",$fetch['questionID'],": ",
        $_POST['score('.$fetch['questionID'].')'],"<br></tr>";
    }
    else{
        echo "<center><font color=red>All fields are required</font><br><br>
        <input type='button' name='return' value='Return' onclick='history.go(-1)'><br>";
        exit();
    }
}

//DISPLAYS QUESTIONS WITH QUESTION NUMBER
echo "<tr align=left><th><font face='Courier New,arial' size=2px>".$fetch["questionID"].". ".$fetch["question"]."<br>
    <td><font face='Courier New,arial' size=2px>";


//DISPLAYS RADIO BUTTONS FROM 1 to 5    
for($i = 1; $i < 6; $i++){
    echo "<input type=radio name='score(".$fetch["questionID"].")' value=",$i," >"; echo $i;
}   

}

2 个答案:

答案 0 :(得分:1)

我会在while循环中放置一个变量来检查错误。如下所示:

$validationFailed = false;
while($fetch=mysql_fetch_array($query)){

//DISPLAY ALL ANSWERS
if(isset($_POST['voted'])){
if(in_array($_POST['score('.$fetch['questionID'].')'], $radioValue)){
    echo "<tr>Question no. ",$fetch['questionID'],": ",
    $_POST['score('.$fetch['questionID'].')'],"<br></tr>";
}
else{
    $validationFailed = true;
    exit();
}
}

//following should be outside the while loop.
if($validationFailed) {
    echo "<center><font color=red>All fields are required</font><br><br>
    <input type='button' name='return' value='Return' onclick='history.go(-1)'><br>";
}

答案 1 :(得分:0)

首先请使用相同名称的单选按钮,价值会有所不同。 现在尝试提交此代码如果为true,则发布您的表单,否则显示错误消息

var iz_checked = true;
$('input').each(function(){
   iz_checked = iz_checked && $(this).is(':checked');
});