Monkey form

from Wikipedia, the free encyclopedia

As a monkey form one is programming approach of HTML - Forms Processing of websites referred to. The HTML form and the validity query are located within a script, and the form therefore calls itself again and again when it is sent, as long as invalid data is entered.

The term monkey form is derived from the fact that even calling the form a million times by a million monkeys , i.e. aping or repeating the unchanged call, will have no effect without meaningful or logical data being specified or meaningless data being changed.

Structural design of a monkey form

approach

The programming approach for a monkey form usually uses a server-side PHP , Python, or Perl script that is located at the beginning of the file or in a separate file and receives the data entered via the Common Gateway Interface (CGI), checking for valid input and shows the form again in the event of incorrect entries. The form fields are pre-filled with the previously entered data and the user can correct or add to his entries.

Technical implementation

  • The first step is to create an HTML form.
  • In the second step, the server-side programming language used ensures that the browser always sends the form to "itself" (i.e. the URL originally called up ). Usually either the script's own name or the environment variable containing the name of the page is entered in the actionattribute of the formtag.
  • Step three then ensures that all form entries appear unchanged when the form is sent as a default in the form fields in the new output.
  • A form in this state is called a monkey form and can be used in the fourth step as the basis for further programming . For example, a check of the form values ​​can be added in order to branch to data processing and storage in the event of error-free data entry - but otherwise display the form again with a corresponding error text.

example

The following simple example of a monkey form shows the necessary code snippets in HTML and PHP, which are usually located within a script.

<?php
// Verarbeitung nur bei abgesendetem Formular starten
if (!empty($_POST)) {

    $fehler = array();

    // Inhaltsprüfungen
    if(empty($_POST['textfeld'])) {
        $fehler['textfeld'] = 'Textfeld darf nicht leer sein';
    }
    // weitere Prüfungen hier einfügen


    if (empty($fehler)) {
        // Verarbeitung war erfolgreich
        // Weiterleiten beispielsweise zu einer Dankesseite
        header("Location: http://example.com/danke.php");
        exit;

    } else {

        // Ausgabe der gesammelten Fehlermeldungen
        echo "<ul class='errors'>\n";
        foreach ($fehler as $feldname => $meldung) {
            printf("<li>%s</li>\n", htmlspecialchars($meldung));
        }
        echo "</ul>\n";
    }
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post">
    <input type="text" name="textfeld"
        value="<?php echo isset($_POST['textfeld']) ? htmlspecialchars($_POST['textfeld']) : ''; ?>" />
    <input type="submit" name="submit" value="Absenden" />
</form>

See also

Web links