programmieren:html:forms
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| programmieren:html:forms [2026/08/12 09:16] – type shi admin | programmieren:html:forms [2026/09/03 14:56] (current) – Mistakes admin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== HTML Forms ====== | ====== HTML Forms ====== | ||
| + | |||
| + | ===== 1st Iteration ===== | ||
| HTML forms done like dis in a PHP app for example: | HTML forms done like dis in a PHP app for example: | ||
| Line 15: | Line 17: | ||
| 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. | 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 ===== | + | ==== Reading data from Form ==== |
| Form needs to do a Post then the data can be read from the post like so: | Form needs to do a Post then the data can be read from the post like so: | ||
| Line 31: | Line 33: | ||
| </ | </ | ||
| + | ===== 2nd Iteration ===== | ||
| + | |||
| + | Added | ||
| + | <code php> | ||
| + | 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 | ||
| + | <code php> | ||
| + | foreach ($_POST as $key => $value) { | ||
| + | if (empty($value)) { | ||
| + | session_start(); | ||
| + | $_SESSION[' | ||
| + | |||
| + | header(' | ||
| + | exit; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | index.php | ||
| + | <code php> | ||
| + | <?php | ||
| + | session_start(); | ||
| + | if (isset($_SESSION[' | ||
| + | <div style=" | ||
| + | <?= htmlspecialchars($_SESSION[' | ||
| + | </ | ||
| + | <?php unset($_SESSION[' | ||
| + | <?php endif ?> | ||
| + | </ | ||
| + | |||
| + | The validation rules I made: | ||
| + | <code php> | ||
| + | if (strlen($post[' | ||
| + | session_start(); | ||
| + | $_SESSION[' | ||
| + | |||
| + | header(' | ||
| + | exit; | ||
| + | } | ||
| + | |||
| + | if (!preg_match('/ | ||
| + | session_start(); | ||
| + | $_SESSION[' | ||
| + | |||
| + | header(' | ||
| + | exit; | ||
| + | } | ||
| + | |||
| + | if (!preg_match( | ||
| + | '/ | ||
| + | $post[' | ||
| + | ) | ||
| + | || strlen($post[' | ||
| + | session_start(); | ||
| + | $_SESSION[' | ||
| + | |||
| + | header(' | ||
| + | exit; | ||
| + | } | ||
| + | |||
| + | if (!date_parse_from_format(' | ||
| + | session_start(); | ||
| + | $_SESSION[' | ||
| + | |||
| + | header(' | ||
| + | exit; | ||
| + | } | ||
| + | |||
| + | if (strlen($post[' | ||
| + | session_start(); | ||
| + | $_SESSION[' | ||
| + | |||
| + | header(' | ||
| + | exit; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== 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): | ||
| + | { | ||
| + | $servername = " | ||
| + | $username = " | ||
| + | $password = ""; | ||
| + | $dbname = " | ||
| + | |||
| + | $conn = new mysqli($servername, | ||
| + | |||
| + | if ($conn-> | ||
| + | die(" | ||
| + | } | ||
| + | |||
| + | $sql = " | ||
| + | |||
| + | $stmt = $conn-> | ||
| + | if ($stmt) { | ||
| + | $stmt-> | ||
| + | |||
| + | $name = $post[" | ||
| + | $email = $post[" | ||
| + | $phone = $post[" | ||
| + | $birthday = $post[" | ||
| + | $message = $post[" | ||
| + | |||
| + | $stmt-> | ||
| + | } else { | ||
| + | echo " | ||
| + | } | ||
| + | |||
| + | $stmt-> | ||
| + | $conn-> | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | And displaying all entries in the index as a list: | ||
| + | |||
| + | <code php> | ||
| + | public function displayAllEntries(): | ||
| + | { | ||
| + | $servername = " | ||
| + | $username = " | ||
| + | $password = ""; | ||
| + | $dbname = " | ||
| + | |||
| + | $conn = new mysqli($servername, | ||
| + | |||
| + | if ($conn-> | ||
| + | die(" | ||
| + | } | ||
| + | |||
| + | $sql = " | ||
| + | $result = $conn-> | ||
| + | |||
| + | if ($result-> | ||
| + | while($row = $result-> | ||
| + | echo 'Name: ' . htmlspecialchars($row[' | ||
| + | } | ||
| + | } else { | ||
| + | echo "0 results"; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== 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.1786518987.txt.gz · Last modified: by admin
