User Tools

Site Tools


programmieren:html:forms

This is an old revision of the document!


HTML Forms

1st Iteration

HTML forms done like dis in a PHP app for example:

<form action="form.php" method="post" id="message">
    Name: <input type="text" name="name"><br>
    E-mail: <input type="email" name="email"><br>
    <input type="submit">
</form>
 
<textarea name="message" form="message">Write message here...</textarea>

This leads to the textarea being below the submit button but still being part of the form since you can't put textareas as a form input.

Reading data from Form

Form needs to do a Post then the data can be read from the post like so:

<?= 'Name: ' . $_POST["name"] ?>
<br>
<?= 'Email: ' . $_POST["email"] ?>
<br>
<?= 'Phone: ' . $_POST["phone"] ?>
<br>
<?= 'Birthday: ' . $_POST["birthday"] ?>
<br>
<?= 'Message: ' . $_POST["message"] ?>

2nd Iteration

Added

htmlspecialchars()

around output so no special chars can be inserted.

Flash Messages

Yoooo I made flash messages, gotta put them in the session (cuz that stays across pages) then send the user back to the index and check in the index for flash messages.

form.php

foreach ($_POST as $key => $value) {
    if (empty($value)) {
        session_start();
        $_SESSION['validation_error'] = $key . " cannot be empty";
 
        header('Location: index.php');
        exit;
    }
}

index.php

<?php
session_start();
if (isset($_SESSION['validation_error'])): ?>
    <div style="color: red">
        <?= htmlspecialchars($_SESSION['validation_error']) ?>
    </div>
    <?php unset($_SESSION['validation_error']) ?>
<?php endif ?>
programmieren/html/forms.1786523018.txt.gz · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki