Website Development Prices

Search Blog

Monday, June 6, 2016

Rukovanje podacima koji dolaze od korisnika preko web formi (Handling the data supplied by the user via web forms)

Da biste u PHP-u mogli da procitate podatke koji dolaze sa HTML kontrola, morate da te kontrole postavite na HTML forme koje se nalaze na web stranama. Ovo se postize pomocu HTML elementa <form>. Najvazniji atributi ovog elementa su:

  • action
  • method
  • target
Atribut action - predstavlja URL koji ce rukovati podacima sa forme. Moze da se izostavi, i u tom slucaju se podrazumeva URL tekuceg dokumenta.

Atribut method - definise metod ili protokol za slanje podataka do ciljnog URL-a. Ako postavite da to bude get, sto je podrazumevana vrednost, metod parove ime/vrednost salje preko URL-a u obliku URL?ime=vrednost&ime=vrednost. Ako postavite da to bude post, onda se sadrzaj forme kodira isto kao kod metoda get, ali se podaci salju skriveno.

Atribut target - pokazuje na okvir (en. frame) u pretrazivacu koji treba da prikaze rezultat.

Primer: ako zelite da procitate podatke koje je korisnik uneo pomocu kontrola na web strani, zelite da koristite PHP skript sa imenom phpcitac.php, koji se nalazi u istom folderu kao phpcitac.html. U tom slucaju atribut action forme treba da postavite na phpcitac.php. Ako phpcitac.php nije u istom folderu, onda morate da zadate njegov URL relativno u odnosu na tekucu stranu ili apsolutno, kao na primer, http://imewebsajta,com/vaseime/phpcitac.php.


<!DOCTYPE html>
<html>
<head>
<title>HTML forme</title>
</head>


<body>
<h1>Upotreba HTML formi</h1>

<form method="post" action="phpcitac.php">
.
.
.
</form>
</body>
</html>


Sada mozete da na HTML formu postavite komtrole, kao sto polja za unos teksta, radio dugmad, oblasti za unos teksta, i drugo. Kada korisnik unese podatke u ove kontrole, svi podaci se salju nazad u phpcitac.php. Ovo nastaje kada korisnik klikne dugme Posaljite. Forme kao sto je ova imaju standardno dugme submit

Primer 2: dodavanje dugmeta tipa submit na formu:


<!DOCTYPE html>
<html>
<head>
<title>HTML forme</title>
</head>

<body>
<h1>Upotreba HTML formi</h1>

<form method="post" action="phpcitac.php">
.
.
.
<input type="submit" value="Posaljite">
</form>
</body>
</html>


Rezultat:

Pored dugmadi tipa submit, mozete koristiti i dugmad tipa reset. Kada se klikne dugme ovog tipa, svi podaci se resetuju na kontrolama na formi. Tada podaci dobijaju svoje prvobitne vrednost, obicno prazna polja, 

Primer 3: dodavanje dugmeta tipa reset na formu:


<!DOCTYPE html>
<html>
<head>
<title>HTML forme</title>
</head>

<body>
<h1>Upotreba HTML formi</h1>

<form method="post" action="phpcitac.php">
.
.
.
<input type="submit" value="Posaljite">
<input type="reset" value="Ponistite">
</form>
</body>
</html>


Rezultat:

Kako pristupiti podacima koji su vam poslati iz PHP skripta?

Ako ste koristili metod get, podatke mozete pronaci u nizu $_GETAko ste koristili metod post, koristite niz $_POST. Ovi nizovi su superglobalni, sto znaci da im se moze pristupiti bez kljucne reci global. Postoji i niz $_REQUEST koji sadrzi podatke i iz niza $_GET i iz niza $_POST.


To be able to read the data in PHP which are coming from an HTML control, you need set those controls on the HTML forms that are found on web pages. This is achieved by using the HTML element <form>. The most important attributes of this element are:
  • action
  • method
  • target
The attribute action - represents a URL that will handle the data from the form. It can be omitted, and in this case it means the URL of the current document.

The attribute method - defines the method or protocol for sending data to the target URL. If you set it to be a get, which is the default value, method pairs name/value sends over a URL in the form of a URL?name=value&name=value. If you set it to be post, then the content of form encodes in the same way as with the get method, but the data is sent hidden.

The attribute target - points to the frame in the browser that should display the result.

Example: if you want to read data typed by a user using the controls on the web page, if you want to use PHP script with the name phpreader.php, located in the same folder as phpreader.html. In this case, the action attribute of the form you need to set to the phpreader.php. If phpreader.php is not in the same folder, then you need to specify a URL relative to the current page or absolutely, as for example, http://websitename.com/yourname/phpreader.php.

<!DOCTYPE html>
<html>
<head>
<title>HTML forms</title>
</head>

<body>
<h1>Using HTML forms</h1>

<form method="post" action="phpreader.php">
.
.
.
</form>
</form>
</body>
</html>

Now you can make an HTML form with controls, such as text fields, radio buttons, textareas, and others. When a user enters data in the control, all data is sent back to phpreader.php. This occurs when the user clicks the Send button. Forms such as these have a standard button submit.

Example 2: adding a button type submit to the form:

<!DOCTYPE html>
<html>
<head>
<title>HTML forms</title>
</head>

<body>
<h1>Using HTML forms</h1>

<form method="post" action="phpreader.php">
.
.
.
<input type="submit" value="Send">
</form>
</form>
</body>
</html>

Result:

In addition to the buttons of type submit, you can use the buttons type reset. When you click a button of this type, all the data is reset to the controls on the form. Then the data receives its beginning values, usually empty fields,

Example 3: adding a button type reset to the form:

<!DOCTYPE html>
<html>
<head>
<title>HTML forms</title>
</head>

<body>
<h1>Using HTML forms</h1>

<form method="post" action="phpreader.php">
.
.
.
<input type="submit" value="Send">
<input type="reset" value="Reset">
</form>
</form>
</body>
</html>

Result:

How to access the data that are sent to you from PHP script?

If you have used method get, the data can be found in array $_GET. If you used the method post, you can use array $_POST. These arrays are superglobal, which means that they can be accessed without the keyword global. There is also array $_REQUEST, which contains information from an array $_GET and from an array $_POST.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.