<?php include_once("conn.php");
function filterTextarea($source) { $source = htmlspecialchars(trim($source)); $source = str_replace("\r\n", "<br>", $source); return $source; } ?> <!doctype html> <html> <head> <title>Form</title> <script> function checkForm() { // stop submitting if the content is empty var textarea = document.getElementById("content"); var content = textarea.value; if (content == ""){ textarea.focus(); return false; } } </script> </head>
<body> <?php if (isset($_POST["content"])) { // if the form has been submitted $content = filterTextarea($_POST["content"]); if (mb_strlen($content) >= 500) { echo "The content is too long."; } else { $sql = "INSERT INTO Contents (Content, TimeCreated) VALUES (:content, NOW())"; $stmt = $dbh->prepare($sql); $stmt->bindParam(":content", $content); $stmt->execute(); if ($stmt->rowCount()) { echo "Your content has been successfully saved."; } else { echo "An error occurred when saving your content."; } } } else { ?> <form onsubmit="return checkForm()" method="post"> <label>Content:</label><br> <textarea style="width:300px;height:100px" id="content" name="content"></textarea><br> <button>Submit</button> </form> <?php } ?> </body> </html>
|