0

I the following code, I have a form that consists of three fields and two buttons. In the Review button, I would like to show any word in Arabic randomly and let the user show its translation in English by ticking the Show translation checkbox.

<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>

Everything works fine except that I don't know how to show the translation of the word when the checkbox is checked.

Do you have any suggestions of how to let that work?

Thanks

Adam Amin
  • 1,406
  • 2
  • 11
  • 23
  • 1
    An unchecked checkbox input will not be sent to the server – Patrick Q Oct 13 '17 at 18:18
  • https://stackoverflow.com/questions/2268887/how-do-i-see-which-checkbox-is-checked – caramba Oct 13 '17 at 18:20
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Oct 13 '17 at 18:25
  • You should provide an example of what the result is that you're trying to achieve. Despite what you actually ask, I'm wondering if maybe you're looking for a Javascript solution here to modify the display when the checkbox is checked. – Patrick Q Oct 13 '17 at 18:28

0 Answers0