PHP Doku:: Formulare verarbeiten - tutorial.forms.html

Verlauf / Chronik / History: (2) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchEinführungKleines TutorialFormulare verarbeiten

Ein Service von Reinhard Neidl - Webprogrammierung.

Kleines Tutorial

<<Nützliches

Alten Code mit neuen PHP-Versionen benutzen>>

Formulare verarbeiten

Eine der mächtigsten Funktionen von PHP ist die Art, wie HTML-Formulare verarbeitet werden. Sie sollten wissen, dass jedes Element eines Formulars automatisch in Ihren PHP-Skripts verfügbar ist. Bitte lesen Sie die Seite Variablen aus externen Quellen für weitere Informationen und Beispiele über das Benutzen von Formularen mit PHP. Hier ist ein Beispiel-HTML-Formular:

Beispiel #1 Ein einfaches HTML-Formular

<form action="action.php" method="post">
 <p>Ihr Name: <input type="text" name="name" /></p>
 <p>Ihr Alter: <input type="text" name="alter" /></p>
 <p><input type="submit" /></p>
</form>

An diesem Formular ist nichts Besonderes. Es ist ein normales HTML-Formular ohne irgendwelche speziellen Tags. Wenn der Benutzer das Formular ausfüllt und den Submit-Button anklickt, wird die Seite action.php aufgerufen. Diese Datei könnte so aussehen:

Beispiel #2 Daten des Formulars ausgeben

Hallo <?php echo $_POST['name']; ?>.
Sie sind <?php echo $_POST['alter']; ?> Jahre alt.

Hallo <?php echo htmlspecialchars($_POST['name']); ?>.
Sie sind <?php echo (int)$_POST['alter']; ?> Jahre alt.

Die Ausgabe des Skripts könnte dann so aussehen:

Hallo Joe.
Sie sind 22 Jahre alt.

Abgesehen von dem htmlspecialchars()-Aufruf und dem (int) Cast sollte einfach zu verstehen sein was hier geschieht. htmlspecialchars() stellt sicher das das Zeichen die in HTML eine spezielle Bedeutung haben ordentlich codiert werden so das niemand HTML Tags oder Javascript-Code in ihre Seite einschmuggeln kann. Da wir wissen das das "alter" Feld eine Zahl enthalten soll konvertieren wir es in einen integer Wert wodurch automatisch überflüssige Zeichen entfernt werden. Sie können diese Aufgabe auch PHP überlassen indem sie die Filter Extension benutzen. Die Variablen $_POST['name'] und $_POST['alter'] werden für Sie automatisch von PHP gesetzt. Weiter oben haben wir das superglobale Array $_SERVER eingeführt, jetzt benutzen wir hier das - ebenfalls superglobale - Array $_POST, dass alle POST-Daten enthält. Beachten Sie, dass die im Formular verwendete Methode POST ist. Hätten wir GET verwendet, dann wären die Daten unseres Formulars stattdessen im superglobalen Array $_GET verfügbar. Sie können auch das superglobale Array $_REQUEST benutzen, wenn die Quelle der Daten keine Rolle spielt. Dieses Array enthält die GET-, POST- und COOKIE-Daten. Beachten Sie auch die import_request_variables()-Funktion.

Sie können auch die Eingaben von XForms in PHP verarbeiten, auch wenn Ihnen die gut von PHP unterstützten HTML-Formulare bisher gereicht haben. Auch wenn die Arbeit mit XForms nichts für Anfänger ist, sind vielleicht trotzdem daran interessant. In unserem Features-Kapitel finden Sie eine kurze Einführung in die Verarbeitung von XForms-Daten.


5 BenutzerBeiträge:
- Beiträge aktualisieren...
Johann Gomes (johanngomes at gmail dot com)
12.10.2010 4:20
Also, don't ever use GET method in a form that capture passwords and other things that are meant to be hidden.
arnel_milan at hotmail dot com
29.03.2008 18:27
I was so shocked that some servers have a problem regarding the Submit Type Name and gives a "Not Acceptable error" or Error 406.

Consider the example below :

<form action="blah.php" method="POST">
  <table>
    <tr>
      <td>Name:</td>
      <td><input type="text" name="name"></td>
    </tr>
   
    <tr>
      <td colspan="2" align="center">
        <input type="submit" name="Submit_btn" id="Submit_btn" value="Send">
      </td>
    </tr> 
  </table>
</form>

This very simple code triggers the "Not Acceptable Error" on
PHP Version 5.2.5 and Apache 1.3.41 (Unix) Server.

However to fix this below is the right code:

<form action="blah.php" method="POST">
  <table>
    <tr>
      <td>Name:</td>
      <td><input type="text" name="name"></td>
    </tr>
   
    <tr>
      <td colspan="2" align="center">
        <input type="submit" name="Submitbtn" id="Submit_btn" value="Send">
      </td>
    </tr> 
  </table>
</form>

The only problem that took me hours to find out is the "_" in the Submit Button.

Hope this help!
SvendK
9.11.2006 5:02
As Seth mentions, when a user clicks reload or goes back with the browser button, data sent to the server, may be sent again (after a click on the ok button).

It might be wise, to let the server handle whatever there is to handle, and then redirect (a redirect is not visible in the history and thus not reachable via reload or "back".

It cannot be used in this exact example, but as Seth also mentions, this example should be using GET instead of POST
yasman at phplatvia dot lv
5.05.2005 9:18
[Editor's Note: Since "." is not legal variable name PHP will translate the dot to underscore, i.e. "name.x" will become "name_x"]

Be careful, when using and processing forms which contains
<input type="image">
tag. Do not use in your scripts this elements attributes `name` and `value`, because MSIE and Opera do not send them to server.
Both are sending `name.x` and `name.y` coordiante variables to a server, so better use them.
sethg at ropine dot com
1.12.2003 21:55
According to the HTTP specification, you should use the POST method when you're using the form to change the state of something on the server end. For example, if a page has a form to allow users to add their own comments, like this page here, the form should use POST. If you click "Reload" or "Refresh" on a page that you reached through a POST, it's almost always an error -- you shouldn't be posting the same comment twice -- which is why these pages aren't bookmarked or cached.

You should use the GET method when your form is, well, getting something off the server and not actually changing anything.  For example, the form for a search engine should use GET, since searching a Web site should not be changing anything that the client might care about, and bookmarking or caching the results of a search-engine query is just as useful as bookmarking or caching a static HTML page.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)