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

search for in the

PHP'nin yeni sürümleri ile eski kod yapısını kullanmak> <İşe yarar bir şey
[edit] Last updated: Fri, 23 Dec 2011

view this page in

Form Kullanımı

PHP'nin en güçlü özelliklerinden biri HTML formlarına yaklaşım biçimidir. Bilinmesi gereken ilk önemli durum, form içindeki tüm elemanların PHP tarafından özdevinimli olarak kullanılabilir olacağıdır. PHP ile formların kullanımı ve daha ayrıntılı bilgi için Dış kaynaklı değişkenler bölümünü okuyabilirsiniz. Örnek bir HTML formu:

Örnek 1 - Örnek bir HTML formu

<form action="action.php" method="post">
 <p>İsminiz: <input type="text" name="isim" /></p>
 <p>Yaşınız: <input type="text" name="yaş" /></p>
 <p><input type="submit" /></p>
</form>

Bu formda özel hiçbir şey yoktur. Hiçbir özel etiket içermeyen düz bir HTML formudur. Kullanıcı formu doldurup gönder tuşuna bastığında, action.php sayfası çağrılır. Bu dosyaya aşağıdakileri yazabiliriz:

Örnek 2 - Formdan veri yazdırmak

Merhaba <?php echo htmlspecialchars($_POST['isim']); ?>.
Siz <?php echo (int)$_POST['yaş']; ?> yaşındasınız.

Bu betikten elde edilecek örnek çıktı:

Merhaba Ahmet. Siz 22 yaşındasınız.

htmlspecialchars() ve (int) kısımları harcinde yapılan iş oldukça açık. htmlspecialchars() işlevi HTML'ye özel karakterlerin doğru şekilde kodlandığından emin olunmasını sağlar, dolayısıyla başkaları sayfanıza dışardan HTML etiketleri veya Javascript yerleştiremez. Yaş alanınında ise değerin bir tamsayı olması gerektiğini bildiğimiz için değeri integer türüne dönüştürmekle özdevinimli olarak bu alana girilmesi olası başı boş karakterlerden de kurtulmuş olduk. Ayrıca, bunun PHP'de sizin yerinize özdevinimli olarak yapılmasını sağlamak için süzgeç eklentisini de kullanabilirdiniz. $_POST['isim'] değişkeni ve $_POST['yaş'] değişkenleri sizin yerinize PHP tarafından özdevinimli olarak oluşturulur. Daha önce $_SERVER süper küresel değişkenini kullanmıştık, yukarıda ise tüm POST verisini içeren $_POST süper küresel değişkenini tanımış olduk. Formumuz için tanımlı yöntemin POST oluşuna dikkat edin. GET yöntemini kullanmış olsaydık, form bilgilerimiz $_GET süper küresel değişkenine atanmış olacaktı. Bunların haricinde, istemciden gelen verinin hangi kaynaktan geldiği sizin için önemli değilse $_REQUEST süper küreselini de kullanabilirdiniz. Bu değişken GET, POST ve COOKIE verilerinin birleşiminden oluşur. Daha fazla bilgi için import_request_variables() işlevini inceleyebilirsiniz.

PHP içinde XForms öğelerini de kullanabilirsiniz, ancak başlangıç aşamasında çok iyi desteklenen HTML formları sizin işinizi görecektir. XForms ile çalışmak yeni başlayanlar için uygun olmasa da, ilginizi çekebilir. XForms ile çalışmak belgesinde bu konu ile ilgili daha fazla bilgi bulabilirsiniz.



add a note add a note User Contributed Notes Form Kullanımı
Johann Gomes (johanngomes at gmail dot com) 11-Oct-2010 07: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-Mar-2008 09: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 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