PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Používanie starého kódu s novými verziami PHP> <Niečo Užitočné
Last updated: Sun, 25 Nov 2007

view this page in

Spracovanie Formulárov

Jedna z najmocnejších vlastností PHP je spôsob, akým narába s HTML formulármi. Základný koncept, ktorý je dôležitý pochopiť je, že každý prvok formulára bude automaticky dostupný vášmu PHP skritpu. Prosím, prečítajte si sekciu manuálu o Premenných z vonka PHP pre viac informácii a príkladov ako používať formuláre s PHP. Tu je príklad HTML formulára:

Example#1 Jednoduchý HTML formulár

<form action="action.php" method="post">
 <p>Tvoje meno: <input type="text" name="name" /></p>
 <p>Tvoj vek: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>

Na tomto formulári nie je nič špeciálne. Je to priamy HTML formulár bez žiadnych špeciálnych tagov akéhokoľvek druhu. Keď užívatel tento formulár vyplní a stlačí tlačidlo submit, zavolá sa stránka action.php. V tomto súbore by ste napísali niečo ako toto:

Example#2 Vypísanie dát z nášho formulára

Ahoj <?php echo $_POST['name']; ?>.
Mas <?php echo $_POST['age']; ?> rokov.

Vzorový výstup tohto skriptu môže byť:

Ahoj Joe. Maš 22 rokov.

Malo by byť zrejmé, čo toto robí. Nič na tom nie je. Premenné $_POST['name'] a $_POST['age'] sa vám automaticky nastavia PHP-čkom. Pred tým sme použili autoglobálu $_SERVER; hore sme len predstavili autoglobálu $_POST, ktorá obsahuje všetky POST dáta. Všimnite si ako sa metóda nášho formulára POST-ne. Ak by sme použili metódu GET, potom by informácia formulára žila v autoglobále $_GET. Môžete tiež použiť autoglobálu $_REQUEST, ak sa nestaráte o zdroj vami požadovaných dát. Obsahuje spojenú informáciu o GET, POST a COOKIE dátach. Tiež pozri funkciu import_request_variables().

V PHP tiež môžete narábať s XForms vstupom, i keď budete na nejaký čas spokojný s dobre podporovanými HTML formulármi. Aj keď práca s XFormami nie je pre začiatočníkov, snáď sa o ne budete zaujímať. Máme tiež krátky úvod k spracovaniu dát prijímanými XFormami v našej sekcii vlastností.



add a note add a note User Contributed Notes
Spracovanie Formulárov
arnel_milan at hotmail dot com
29-Mar-2008 10: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!
Lord Pacal
03-Jan-2008 06:38
One thing that tripped me up when I was first learning PHP was the use of the NAME attribute in form fields. The current convention is to use the ID attribute instead when creating forms. (Many HTML editors automatically include an ID attribute without a NAME attribute.) Now, I include both the NAME and ID attributes (with the same value) in all my form fields.

For example...
<form method="post">
<input type="text" id="field1">
<input type="submit" value="go">
</form>

If you then have a PHP page requesting the contents of the "field1" field...
<?php echo $_POST["field1"] ?>
...the above form will always return an empty string for "field1".

The solution is to include the NAME attribute...
<form method="post">
<input type="text" id="field1" name="field1">
<input type="submit" value="go">
</form>

With this change, the PHP code will correctly retrieve the value of the "field1" field.
SvendK
08-Nov-2006 08: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
05-May-2005 12: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
01-Dec-2003 12: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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites