php - 使用 PHP 和 jQuery 使用单选按钮进行测验

标签 php jquery html yahoo bing

我正在尝试使用单选按钮进行测验,但我不知道如何在单击按钮后发布答案我添加了一些评论,我在代码末尾解释了这一点。

    <!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
</head>
<body>

<?php
echo "<b> QUIZZZ!!!!! </b> <br>";
echo "----------------------------------------------------------------<br>";
 $k=1;
 $filmat = array("Horror","Aksion","Komedi","Romance","Thriller","Fantazi");
for ($i=0; $i <=5 ; $i++) { 

    echo "$k) Do u like  {$filmat[$i]} movies?";// willl genearate questions from  1->6
    echo "<br>";
    echo"<input type='radio' id='butonYes$k'>Yes";// will generate buttons with  id from 1->6
    echo "<br>";
    echo "<input type='radio' id='butonNo$k'> No";$k++;
    echo "<br>";
}

echo "----------------------------------------------------------------<br>";
echo "the films u selected are: <br>";

// here i want some jquery or php code to print the question with which genere of movies are clicked 
//for exaple i want just to print
/*
Horror
Aksion
Fantazi
*/
// if there are the only ones that are cliked yes
//pls help
?>
</body>
</html>

最佳答案

要实现这一点,您应该注意几件事。首先,您需要将输入元素包装到一个表单标签中。然后在显示结果之前让页面检查是否有 POST 数据。单选输入标签最好按其名称属性分组,而不是让它们都具有唯一 ID。

<?php
    echo "<b> QUIZZZ!!!!! </b> <br>";
    echo "----------------------------------------------------------------<br>";
    echo "<form method='POST' action=''>";
    $k=1;
    $filmat = array("Horror","Aksion","Komedi","Romance","Thriller","Fantazi");
        for ($i=0; $i <=5 ; $i++) { 
            echo "$k) Do u like  {$filmat[$i]} movies?";// willl genearate questions from  1->6
            echo "<br>";
            echo"<input type='radio' name='{$filmat[$i]}' value='yes'>Yes";// will generate buttons with  id from 1->6
            echo "<br>";
            echo "<input type='radio' name='{$filmat[$i]}' value='no'> No";$k++;
            echo "<br>";
    }
    echo "<input type='submit'>";
    echo "</form>";
    echo "----------------------------------------------------------------<br>";
    echo "the films u selected are: <br>";

    // do something with the results here
    if(isset($_POST) && !empty($_POST)) {
        echo "<pre>;";
        print_r($_POST);
    }
?>

关于php - 使用 PHP 和 jQuery 使用单选按钮进行测验,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23037557/

相关文章:

php - 我如何移动多个文件,其中文件名和路径位于数据库表的列中?

php - 显示序列化数组

在 PHP 内部调用时,Javascript 函数没有响应。 (试图将项目添加到选项列表框)

jquery - 导航栏在到达页面顶部后保持在顶部

javascript - 停止表内的链接将表行标记为选定

html - 将 CSS/HTML 中的按钮与文本水平对齐一行?

未应用 JQuery themeroller 悬停

php - 通过日期过滤获取出现次数

javascript - "Array to string conversion error"尝试获取字段值时

PHP 使用 DOMDocument 从 URL 中检索内部 HTML 作为字符串