Website Development Prices

Website Development Prices

Tuesday, November 1, 2016

Upotreba jednog PHP dokumenta (Using one PHP document)

Do sada smo koristili web aplikacije koje su imale dva dokumenta. Jedan sa HTML kodom koji je prikazivao stranu za prikupljanje podataka od korisnika, a drugi sa PHP kodom cija strana je interpretirala i rukovala tim podacima.

Primer: ako zelite da od korisnika saznate koji je njegov omiljen motor, a da onda zelite da tom ime prikazete. Sve to treba da uradite u jednom PHP dokumentu. Da biste sve to stavili na jedan dokument, treba da mozete da odredite da li je to prvi dolazak na dokument tj. stranu. Ako jeste, treba da prikazete stranu za prikupljanje podataka. Ako je korisnik vec uneo svoj omiljen motor, treba da to procitate.

Upotrebicemo polje za unos teksta "ime-motora" i tu cemo da stavimo ime omiljenog motora. Ako tu postoje podaci. onda ime treba da se prikaze. Ako ne postoje podaci, treba da prikazete polje za unos teksta i da od korisnika zatrazite da unese ime omiljenog motora.

Prekopiracemo fajl phptekst.php iz clanka "Preuzimanje podataka iz polja za unos teksta (Retrieving data from text fields)", koji se nalazi u folderu htdocs, foldera xampp. U mom primeru, putanja je C:/xampp2/htdocs/PHPtuts/polje-za-unos-teksta/phptekst.php i kopirati u novo kreirani folder jedan-php-dokument.


<!DOCTYPE html>
<html>
<head>
<title>Web forme i validacija unosa korisnika</title>
</head>

<body>
<h1>Upotreba jednog PHP dokumenta</h1>

<?php
if(isset($_POST["ime-motora"])){
?>

Ime vaseg omiljenog motora je

<?php
echo $_POST["ime-motora"];
}
else {
?>

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

<?php
}
?>

</body>

</html>

Sacuvajte ovaj fajl kao jedan-php-dokument.php, u folderu htdocs, foldera xampp. U mom primeru, putanja je C:/xampp2/htdocs/PHPtuts/jedan-php-dokument/jedan-php-dokument.php.




Rezultat:







So far we have used web applications that had two documents. One with the HTML code that displayed page to collect data from users, and the other with PHP code whose page interpreted and handled the data.

Example: if you want from user to find out what his favorite motorcycle is, and then you want to display the name. All that you have to do in one PHP document. To put it all in one document, you should determine whether this is the first arrival on the document ie. the page. If so, you should show the page for data collection. If the user has already entered his favorite motorcycle, you should read it.

We will use the text field "motorcycle-name" and we're going to put the name of favorite motorcycle. If there is data, then the name should be shown. If there is no data you should show the text field, and to request from the user to enter the name of favorite motorcycle.

We will copy file phptext.php from article "Retrieving data from text fields", which is located in htdocs folder, of the folder xampp. In my example, the path is C:/xampp2/htdocs/PHPtuts/text-field/phptext.php and paste it to the newly created folder one-php-document.

<!DOCTYPE html>
<html>
<head>
<title>Web forms and validation of user input</title>
</head>

<body>
<h1>Using one PHP document</h1>

<?php
if(isset($_POST["motorcycle-name"])){
?>

Name of your favorite motorcycle is

<?php
echo $_POST["motorcycle-name"];
}
else {
?>

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

<?php
}
?>

</body>

</html>

Save this file as one-php-document.php, in the htdocs folder, of the folder xampp. In my example, the path is C:/xampp2/htdocs/PHPtuts/one-php-document/one-php-document.php.




Result:




No comments:

Post a Comment

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