如何验证是否在PHP中选中了复选框

时间:2017-10-13 18:16:57

标签: php html

我在下面的代码中,我有一个由三个字段和两个按钮组成的表单。在Review按钮中,我想用阿拉伯语随机显示任何单词,并让用户通过勾选Show translation复选框显示英文翻译。

<html>
<body>

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $english = $_POST["english"];
        $arabic = $_POST["arabic"];
        $example = $_POST["example"];
    }
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <textarea name="english" rows="4" cols="70" placeholder="English">English</textarea>
    <br>
    <textarea name="arabic" rows="4" cols="70" placeholder="Arabic">Arabic</textarea>
    <br>
    <textarea name="example" rows="4" cols="70" placeholder="Example">Example</textarea>

    <br><br>

    <input type="submit" name="add" value="Add new">
    <input type="submit" name="review" value="Review">
</form>

<?php

$servername = "localhost";
$username = "xxx";
$password = "yyy";
$dbname = "vdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['add'])) {
    $sql = "INSERT INTO Vocabulary (English, Arabic, Example)
    VALUES ('$english', '$arabic', '$example')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
elseif (isset($_POST['review'])) {
    $sql = "SELECT COUNT(ID) as total FROM Vocabulary";
    $result = $conn->query($sql);
    $row = $result->fetch_assoc();
    #echo $row['total'];

    $generated = rand(1,$row['total']);

    $sql1 = "SELECT * FROM Vocabulary where ID = $generated";
    $result1 = $conn->query($sql1);
    $row1 = $result1->fetch_assoc();

    echo "<br>";
    echo $row1['Arabic'];

    echo "<br><br>";
    echo "<input type='checkbox' name='feeling' value='good'>Show translation";

    echo "<br><br>";

}

$conn->close();
?>

</body>
</html>

一切正常,但我不知道在选中复选框时如何显示单词的翻译。

您对如何让这项工作有任何建议吗?

由于

0 个答案:

没有答案