Website Development Prices

Search Blog

Thursday, June 9, 2016

Preuzimanje podataka iz polja za unos teksta (Retrieving data from text fields)

U prethodnom clanku smo napravili web stranicu sa poljem za unos teksta na formi. Ova stranica salje sadrzaj polja za unos teksta skriptu phptekst.php, sto se desava kada korisnik klikne dugme Posaljite.

Primer: da biste preuzeli podatke sa web stranice, upotrebite niz $_REQUEST, koji sadrzi sve podatke iz nizova $_GET i $_POST, sto znaci da ovaj niz mozete koristiti i sa $_GET i sa $_POST metodima. Na stranici phptekst.html polju za unos teksta smo dali ime "ime-motora", pomocu atributa name. Vrednost unetu u ovom polju mozemo da preuzmemo pomocu iskaza $_REQUEST["ime-motora"].

HTML kod je isti kao u prethodnom clanku.

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


<body>
<h1>Upotreba polja za unos teksta</h1>

<form method="post" action="phptekst.php">
<h3>Koji je vas omiljen motor?</h3>
<input type="text" name="ime-motora">
<br /><br />
<input type="submit" value="Posaljite">
</form>
</body>
</html>

Sacuvajte ovaj fajl kao phptekst.html, u folderu htdocs, foldera xampp. U mom primeru, putanja je C:/xampp2/htdocs/PHPtuts/polje-za-unos-teksta/phptekst.html.

PHP kod - opcija 1

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

<body>
<h1>Preuzimanje podataka iz polja za unos teksta</h1>

<?php
$ime = $_REQUEST['ime-motora'];
echo "Vas omiljen motor je $ime";
?>

</body>

</html>

Sacuvajte ovaj fajl kao phptekst.php u istom folderu kao i phptekst.html.

Rezultat:





HTML kod - opcija 2

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

<body>
<h1>Upotreba polja za unos teksta</h1>

<form method="post" action="phptekst2.php">
<h3>Koji je vas omiljen motor?</h3>
<input type="text" name="ime-motora">
<br /><br />
<input type="submit" value="Posaljite">
</form>
</body>

</html>

PHP kod - opcija 2


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

<body>
<h1>Preuzimanje podataka iz polja za unos teksta</h1>

Vas omiljen motor je

<?php
echo $_REQUEST["ime-motora"];
?>

</body>

</html>

Sacuvajte ovaj fajl kao phptekst2.php u istom folderu kao i phptekst.html.

Rezultat:






HTML kod je isti kao kod opcije 1, s tim sto treba da promenite vrednost atributa method sa post na get.

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

<body>
<h1>Upotreba polja za unos teksta</h1>

<form method="get" action="phptekst.php">
<h3>Koji je vas omiljen motor?</h3>
<input type="text" name="ime-motora">
<br /><br />
<input type="submit" value="Posaljite">
</form>
</body>

</html>

PHP kod - opcija 3 - $_GET

U fajlu phptekst.php promenite $_REQUEST na $_GET.

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

<body>
<h1>Preuzimanje podataka iz polja za unos teksta</h1>

<?php
$ime = $_GET['ime-motora'];
echo "Vas omiljen motor je $ime";
?>

</body>

</html>

Rezultat u address baru:

http://localhost/PHPtuts/polje-za-unos-teksta/phptekst.php?ime-motora=KTM

Rezultat:






HTML kod je isti kao kod opcije 1.


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

<body>
<h1>Upotreba polja za unos teksta</h1>

<form method="post" action="phptekst.php">
<h3>Koji je vas omiljen motor?</h3>
<input type="text" name="ime-motora">
<br /><br />
<input type="submit" value="Posaljite">
</form>
</body>

</html>

PHP kod - opcija 4 - $_POST


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

<body>
<h1>Preuzimanje podataka iz polja za unos teksta</h1>

<?php
$ime = $_POST['ime-motora'];
echo "Vas omiljen motor je $ime";
?>

</body>

</html>

Rezultat u address baru:

http://localhost/PHPtuts/polje-za-unos-teksta/phptekst.php

Rezultat:






Napomena: nemojte da otvarate fajl phptekst.html iz foldera, u mom primeru, polje-za-unos-teksta


otvorite ga direktno sa localhost-a



Ako otvorite iz file:///C:/xampp2/htdocs/PHPtuts/polje-za-unos-teksta/phptekst.html



dobicete praznu stranu



In the previous article we have made a web page with a text field on the form. This page sends the contents of a text field, to the script phptext.php, it happens when the user clicks the Send button.

Example: to retrieve data from a web page, use an array $_REQUEST, which contains all the data from the arrays $_GET and $_POST, which means that this array can also be used with the $_GET and $_ POST methods. On page phptext.html we gave the name "motorcycle-name" to the text field, using the attribute name. The value entered in this field, we can retrieve using statement  $_ REQUEST [ 'motorcycle-name"].

HTML code is the same as in the previous article.

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

<body>
<h1>Using a text fields</h1>

<form method="post" action="phptext.php">
<h3>What's your favorite motorcycle?</h3>
<input name="motorcycle-name" type="text">
<br /><br />
<input type="submit" value="Send">
</form>

</body>

</html>

Save this file as phptext.html, in the htdocs folder, in the folder xampp. In my example, the path is C:/xampp2/htdocs/PHPtuts/text-field/phptext.html.

PHP code - option 1

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

<body>
<h1>Retrieving data from text fields</h1>

<?php
$name = $_REQUEST['motorcycle-name'];
echo "Your favorite motorcycle is $name";
?>

</body>

</html>

Save this file as phptext.php in the same folder as the phptext.html.

Result:




HTML - option 2

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

<body>
<h1>Using a text fields</h1>

<form method="post" action="phptext2.php">
<h3>What's your favorite motorcycle?</h3>
<input name="motorcycle-name" type="text">
<br /><br />
<input type="submit" value="Send">
</form>

</body>

</html>

PHP code - option 2

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

<body>
<h1>Retrieving data from text fields</h1>

Your favorite motorcycle is

<?php
echo $_REQUEST["motorcycle-name"];
?>

</body>

</html>


Save this file as phptext2.php in the same folder as the phptext.html.

Result:





HTML code is the same as in option 1, except that you need to change the value of attribute method from post to get.

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

<body>
<h1>Using a text fields</h1>

<form method="get" action="phptext2.php">
<h3>What's your favorite motorcycle?</h3>
<input name="motorcycle-name" type="text">
<br /><br />
<input type="submit" value="Send">
</form>

</body>

</html>


PHP code - option 3 - $_GET

In the file phptext.php change from $_REQUEST to $_GET.

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

<body>
<h1>Retrieving data from text fields</h1>

<?php
$name = $_GET['motorcycle-name'];
echo "Your favorite motorcycle is $name";
?>

</body>

</html>

The result in the address bar:

http://localhost/PHPtuts/text-field/phptext2.php?motorcycle-name=ktm

Result:







HTML code is the same as in the option 1

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

<body>
<h1>Using a text fields</h1>

<form method="post" action="phptext.php">
<h3>What's your favorite motorcycle?</h3>
<input name="motorcycle-name" type="text">
<br /><br />
<input type="submit" value="Send">
</form>

</body>

</html>


PHP code - option 4 - $_ POST

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

<body>
<h1>Retrieving data from text fields</h1>

<?php
$name = $_POST['motorcycle-name'];
echo "Your favorite motorcycle is $name";
?>

</body>

</html>


The result in the address bar:

http://localhost/PHPtuts/text-field/phptext.php


Result:



Note: do not open the file phptext.html from a folder, in my example, text-field



open it directly from localhost




If you open from file:///C:/xampp2/htdocs/PHPtuts/text-field/phptext.html




you will get empty page




No comments:

Post a Comment

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