Website Development Prices

Search Blog

Friday, June 17, 2016

Preuzimanje podataka iz lista (Retrieving data from lists)

Liste se prave pomocu kontrole <select> i podrzavaju izbor vise opcija.

Primer: trazicemo od korisnika da izabere omiljena turisticka mesta.

HTML kod - opcija 1

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



<body>

<h2>Upotreba lista</h2>
<form action="php-liste.php" method="post">
<h3>Izaberite vasa omiljena turisticka mesta:</h3>
<select name="turisticko_mesto[]" multiple="true">
<option>Split</option>
<option>Pula</option>
<option>Makarska</option>
<option>Vrsar</option>
<option>Opatija</option>
</select>
<br /><br />
<input name="posaljite" type="submit"  Value="Posaljite"/>
</form>

</body>

</html>

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

Rezultat:



Listi smo dali ime turisticka_mesta, ali posto je moguce izabrati vise opcija, rezultat ne mozemo da dobije pomocu iskaza $_POST["turisticka_mesta"]. Zato treba da trazimo prvu izbranu opciju, ako postoji, pa drugu, ako postoji. Da bismo uhvatili sve opcije upotrebicemo petlju foreach.

PHP kod - opcija 1

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

<body>
<h1>Preuzimanje podataka iz lista</h1>

Izabrali ste:<br /><br />

<?php
foreach($_POST["turisticko_mesto"] as $omiljeno) {
echo $omiljeno, "<br />";
}

?>

</body>

</html>

Sacuvajte ovaj fajl kao php-liste.php, u istom folderu kao i php-liste.html.

Rezultat:



HTML kod - opcija 2

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

</head>
<body>

<h2>Upotreba lista</h2>
<form action="php-liste-2.php" method="post">
<h3>Izaberite vase omiljeno turisticko mesto:</h3>
<select name="turisticko_mesto">
<option value="Split">Split</option>
<option value="Pula">Pula</option>
<option value="Makarska">Makarska</option>
<option value="Vrsar">Vrsar</option>
<option value="Opatija">Opatija</option>
</select>
<br /><br /><br />
<input name="posaljite" type="submit"  Value="Posaljite"/>
</form>


</body>

</html>

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

Rezultat:




PHP kod - opcija 2


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

<body>
<h1>Preuzimanje podataka iz lista</h1>

Izabrali ste:<br /><br />

<?php
echo $_POST["turisticko_mesto"];
?>

</body>

</html>

Sacuvajte ovaj fajl kao php-liste-2.php, u istom folderu kao i php-liste-2.html.

Rezultat: 



Lists are made using the control <select> and they have possibility of the selection of more items.

Example: we will ask the user to choose a favorite tourist spots.

HTML code - option 1

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

</head>
<body>

<h2>Using lists</h2>
<form action="php-lists.php" method="post">
<h3>Choose your favorite tourist countries:</h3>
<select name="tourist_countries[]" multiple="true">
<option>Croatia</option>
<option>Canada</option>
<option>USA</option>
<option>Ireland</option>
<option>Spain</option>
</select>
<br /><br />
<input name="send" type="submit"  Value="Send"/>
</form>

</body>

</html>



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

Result:



We gave name to the list - tourist_spots, but since it is possible to choose more options, we can not obtain result by statement $_POST["tourist_spots"]. So we should seek first selected option, if any, and then the second, if any. To catch all the options we will use foreach loop.

PHP code - option 1

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

<body>
<h1>Retrieving data from lists</h1>

You chose:<br /><br />

<?php
foreach($_POST["tourist_countries"] as $favorite) {
echo $favorite, "<br />";
}

?>

</body>

</html>

Save this file as a php-lists.php, in the same folder as the php-lists.html.

Result:



HTML code - option 2

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

</head>
<body>

<h2>Using lists</h2>
<form action="php-lists-2.php" method="post">
<h3>Choose your favorite tourist country:</h3>
<select name="tourist_country">
<option value="select">Select</option>
<option value="Croatia">Croatia</option>
<option value="Canada">Canada</option>
<option value="USA">USA</option>
<option value="Ireland">Ireland</option>
<option value="Spain">Spain</option>
</select>
<br /><br /><br />
<input name="send" type="submit"  Value="Send"/>
</form>


</body>

</html>

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

Result:




PHP code - option 2


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

<body>
<h1>Retrieving data from lists</h1>

You chose:<br /><br />

<?php
echo $_POST["tourist_country"];
?>

</body>

</html>

Save this file as a php-lists-2.php, in the same folder as the php-lists-2.html.

Result: 



No comments:

Post a Comment

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