User Tools

Site Tools


programmieren:html:forms

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programmieren:html:forms [2026/08/12 10:23] – flash meshuggas trust adminprogrammieren:html:forms [2026/09/03 14:56] (current) – Mistakes admin
Line 70: Line 70:
 <?php endif ?> <?php endif ?>
 </code> </code>
 +
 +The validation rules I made:
 +<code php>
 +if (strlen($post['name']) > 50) {
 +    session_start();
 +    $_SESSION['validation_error'] = "Name cannot be longer than 50 characters.";
 +
 +    header('Location: index.php');
 +    exit;
 +}
 +
 +if (!preg_match('/[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/', $post['email']) || strlen($post['email']) > 50) {
 +    session_start();
 +    $_SESSION['validation_error'] = "Email needs to be a valid email address and no longer than 50 characters.";
 +
 +    header('Location: index.php');
 +    exit;
 +}
 +
 +if (!preg_match(
 +    '/^(\+?)(\d{2,4})(\s?)(-?)((\(0\))?)(\s?)(\d{2})(\s?)(-?)(\d{3})(\s?)(-?)(\d{2})(\s?)(-?)(\d{2})/',
 +    $post['phone']
 +)
 +|| strlen($post['phone']) > 20) {
 +    session_start();
 +    $_SESSION['validation_error'] = "Phone needs to be a valid phone number.";
 +
 +    header('Location: index.php');
 +    exit;
 +}
 +
 +if (!date_parse_from_format('Y-m-d', $post['birthday']) || $post['birthday'] > date('Y-m-d')) {
 +    session_start();
 +    $_SESSION['validation_error'] = "Birthday needs to be a date in the format {yyyy-mm-dd} and cannot be in the future.";
 +
 +    header('Location: index.php');
 +    exit;
 +}
 +
 +if (strlen($post['message']) > 500) {
 +    session_start();
 +    $_SESSION['validation_error'] = "Message cannot be longer than 500 characters.";
 +
 +    header('Location: index.php');
 +    exit;
 +}
 +</code>
 +
 +==== 3rd Iteration ====
 +
 +In the 3rd Iteration I simply added saving the data put into the form to the Database:
 +
 +<code php>
 +    public function savePostData($post): void
 +    {
 +        $servername = "localhost";
 +        $username = "root";
 +        $password = "";
 +        $dbname = "form_exercise";
 +
 +        $conn = new mysqli($servername, $username, $password, $dbname);
 +
 +        if ($conn->connect_error) {
 +            die("Connection failed: " . $conn->connect_error);
 +        }
 +
 +        $sql = "INSERT INTO entries (name, email, phone, birthday, message) VALUES (?, ?, ?, ?, ?)";
 +
 +        $stmt = $conn->prepare($sql);
 +        if ($stmt) {
 +            $stmt->bind_param("sssss", $name, $email, $phone, $birthday, $message);
 +
 +            $name = $post["name"];
 +            $email = $post["email"];
 +            $phone = $post["phone"];
 +            $birthday = $post["birthday"];
 +            $message = $post["message"];
 +
 +            $stmt->execute();
 +        } else {
 +            echo "Error: " . $sql . "<br>" . $conn->error;
 +        }
 +
 +        $stmt->close();
 +        $conn->close();
 +    }
 +</code>
 +
 +And displaying all entries in the index as a list:
 +
 +<code php>
 +    public function displayAllEntries(): void
 +    {
 +        $servername = "localhost";
 +        $username = "root";
 +        $password = "";
 +        $dbname = "form_exercise";
 +
 +        $conn = new mysqli($servername, $username, $password, $dbname);
 +
 +        if ($conn->connect_error) {
 +            die("Connection failed: " . $conn->connect_error);
 +        }
 +
 +        $sql = "SELECT * FROM entries";
 +        $result = $conn->query($sql);
 +
 +        if ($result->num_rows > 0) {
 +            while($row = $result->fetch_assoc()) {
 +                echo 'Name: ' . htmlspecialchars($row['name']) . '  -  Email: ' . htmlspecialchars($row['email']) . '  -  Phone: ' . htmlspecialchars($row['phone']) . '  -  Birthday: ' . htmlspecialchars($row['birthday']) . '  -  Message: ' . htmlspecialchars($row['message']) . "<br>";
 +            }
 +        } else {
 +            echo "0 results";
 +        }
 +    }
 +</code>
 +
 +==== Mistakes ====
 +
 +Code style:
 +
 +  * Apperantly I had too much repetitive code
 +  * Making it object oriented was good
 +
 +Functionality:
 +
 +  * Apperantly it was just barely reached, no explanation as to why.
 +  * I couldve done better validation
 +  * I couldve displayed multiple validation messages for all that was wrong
 +  * I couldve used PHP native validation functions
 +  * I couldve positioned the Form better
 +
 +Presenting:
 +
 +  * I was unprepared
 +  * I didn't answer many questions
 +  * There were some incidents with the streaming so that was bad blegh
programmieren/html/forms.1786523018.txt.gz · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki