使用PHP获取动态创建的选定单选按钮值

时间:2017-03-01 18:25:34

标签: php mysql

我正在尝试获取所选单选按钮的值。这些值是从绑定到控件的MySQL数据库中获取的。程序执行时,当我检查选定的单选按钮值时,它返回null。 非常感谢帮助。提前谢谢。

我文件的片段如下:

HTML:

<form method="POST" action="">
    <h3><?php echo $_SESSION['Question'] ?></h3>
    <input type="radio" name="radio" value="<?php $_SESSION['Option1'] ?>"><?php echo $_SESSION['Option1'] ?><br/>
    <input type="radio" name="radio" value="<?php $_SESSION['Option2'] ?>"><?php echo $_SESSION['Option2'] ?><br/>
    <input type="radio" name="radio" value="<?php $_SESSION['Option3'] ?>"><?php echo $_SESSION['Option3'] ?><br/>
    <input type="radio" name="radio" value="<?php $_SESSION['Option4'] ?>"><?php echo $_SESSION['Option4'] ?><br/>
    </div>
    </div>
    <div class="row">
        <input type="submit" class="btn btn-success" name="button_start" value="Start"/>
        <input type="submit" class="btn btn-success" name="button_Back" value="Back"/>
        <input type="submit" class="btn btn-success" name="button_save" value="Save"/>
        <input type="submit" class="btn btn-success" name="button_Next" value="Next"/>
        <input type="submit" class="btn btn-danger" name="button_Submit" value="Submit"/>
    </div>
</form>

PHP:

function Save($question_id)
{
    if (isset($_POST['button_save'])) {
        if (isset($_POST['radio'])) {
            $selectedValue = $_POST['radio'];
            if (is_null($selectedValue) || $selectedValue == "") {
                echo "Please select an option";
            } else {
                $connectionString = mysqli_connect("localhost", "root", "", "knowellaptitudetest");
                if (!$connectionString) {
                    echo "Error unable to connect to MySQL" . PHP_EOL;
                    echo "Debugging error no." . mysqli_connect_errno() . PHP_EOL;
                    echo "Debugging error" . mysqli_connect_error() . PHP_EOL;
                    exit;
                }
                if ($selectedValue == $_SESSION['Correct_Answer']) {
                    //save and update marks for correct ans
                    $marks = 1;
                    $query = "INSERT INTO section_marks VALUES('" . $question_id . "','" . $marks . "')";
                    $result = mysqli_query($connectionString, $query);
                    $row = mysqli_fetch_row($result, MYSQLI_ASSOC);
                    $count = mysqli_num_rows($result);
                } else {
                    //save and update marks for wrong answer
                    $marks = -1;
                    $query = "INSERT INTO section_marks VALUES('" . $row['Question_ID'] . "','" . $marks . "')";
                    $result = mysqli_query($connectionString, $query);
                    $row = mysqli_fetch_row($result, MYSQLI_ASSOC);
                    $count = mysqli_num_rows($result);
                }
                mysqli_close($connectionString);
            }
        }

1 个答案:

答案 0 :(得分:1)

你必须在html中回显php变量。

 <input type="radio" name="radio" value="<?php echo $_SESSION['Option1'] ?>"><?php echo $_SESSION['Option1'] ?><br/>