Website Development Prices

Search Blog

Saturday, June 11, 2016

Preuzimanje podataka iz polja za potvrdu (Retrieving data from checkboxes)

Polje za potvrdu se pravi pomocu HTML elementa <input type="checkbox">

Primer: pitacemo korisnika da li ima motor. 

HTML kod

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


<body>
<h1>Upotreba polja za potvrdu</h1>

<form method="post" action="php-polje-za-potvrdu.php">
<h3>Da li imate motor?</h3>

<input name="proveri1" type="checkbox" value="Da">
Da<br />
<input name="proveri2" type="checkbox" value="Ne">
Ne<br />

<br /><br />
<input type="submit" value="Posaljite">
</form>
</body>

</html>

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

Rezultat:




Pomocu iskaza $_POST["proveri1"] i $_POST["proveri2"] mozete da odredite koje je polje korisnik izabrao.

PHP kod

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

<body>
<h1>Upotreba polja za potvrdu</h1>

<form method="post" action="php-polje-za-potvrdu.php">
<h3>Da li imate motor?</h3>

<input name="proveri1" type="checkbox" value="Da">
Da<br />
<input name="proveri2" type="checkbox" value="Ne">
Ne<br />

<br /><br />
<input type="submit" value="Posaljite">
</form>
</body>

</html>

Sacuvajte ovaj fajl kao php-polje-za-potvrdu.php, u istom folderu kao i  php-polje-za-potvrdu.html.

Rezultat:




Primer 2: pitacemo korisnika koje sve motore ima. Atributu name dacemo ime proveri_motor[] i moci cemo da pristupimo svim poljima za potvrdu uz pomoc niza ($_POST['proveri_motor'][])Koristicemo foreach petlju da prikazemo pojedinacne vrednosti izabranih polja za potvrdu. Upotrebicemo funkciju count da izbrojimo broj izabranih polja za potvrdu.

HTML kod

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

</head>
<body>

<h2>Upotreba polja za potvrdu</h2>
<form action="php-polje-za-potvrdu-2.php" method="post">
<h3>Izaberite motore koje imate:</h3>
<input name="proveri_motor[]" type="checkbox" value="Suzuki">Suzuki<br />
<input name="proveri_motor[]" type="checkbox" value="KTM">KTM<br />
<input name="proveri_motor[]" type="checkbox" value="Honda">Honda<br />
<input name="proveri_motor[]" type="checkbox" value="Ducati">Ducati<br />
<input name="proveri_motor[]" type="checkbox" value="Yamaha">Yamaha<br /><br />
<input name="posaljite" type="submit"  Value="Posaljite"/>
</form>

</body>

</html>

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

Rezultat:




PHP kod

<?php
if(isset($_POST['posaljite']))
{

if(!empty($_POST['proveri_motor'])) 
{
$izabrani_broj = count($_POST['proveri_motor']);

echo "Izabrali ste sledeci(a) ".$izabrani_broj." motor(a): <br/>";

foreach($_POST['proveri_motor'] as $izabrano) 
{
echo "<p>".$izabrano ."</p>";
}
}
else{
echo "Izaberite makar jednu opciju.";
}
}


?>

Sacuvajte ovaj fajl kao php-polje-za-potvrdu-2.php, u istom folderu kao i php-polje-za-potvrdu-2.html.

Rezultat:




Ako ne izbarete nijednu opciju




Rezultat:



The checkbox is made using HTML element <input type="checkbox">.

Example: we will ask the user whether he has the motorcycle.

HTML code

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

<body>
<h1>Using checkboxes</h1>

<form method="post" action="php-checkbox.php">
<h3>Do you have a motorcycle?</h3>

<input name="check1" type="checkbox" value="Yes">
Yes<br />
<input name="check2" type="checkbox" value="No">
No<br />

<br /><br />
<input type="submit" value="Send">
</form>
</body>

</html>

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

Result:




Using statement $_POST["check1"] and $_POST["check2"] you can determine which checkbox user selected.

PHP code

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

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

You chose

<?php
if (isset($_POST["check1"]))
echo $_POST["check1"], "<br />";
if (isset($_POST["check2"]))
echo $_POST["check2"], "<br />";
?>

</body>

</html>

Save this file as php-checkbox.php, in the same folder as the php-chechbox.html.

Result:



Example 2: we'll ask the user what motorcycles he has. To attribute name we'll give the name check_motorcycles[] and we will be able to access all the checkboxes with the help of an array ($_POST['check_motorcycles'][]). We will use the foreach loop to display a single value of selected checkboxes. We will use count function to count the number of selected checkboxes.

HTML code

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

</head>
<body>

<h2>Using checkboxes</h2>
<form action="php-html-checkbox.php" method="post">
<h3>Choose motorcycles that you have:</h3>
<input name="check_motorcycle[]" type="checkbox" value="Suzuki">Suzuki<br />
<input name="check_motorcycle[]" type="checkbox" value="KTM">KTM<br />
<input name="check_motorcycle[]" type="checkbox" value="Honda">Honda<br />
<input name="check_motorcycle[]" type="checkbox" value="Ducati">Ducati<br />
<input name="check_motorcycle[]" type="checkbox" value="Yamaha">Yamaha<br /><br />
<input name="send" type="submit"  Value="Send"/>
</form>

</body>

</html>

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

Result:



PHP code

<?php
if(isset($_POST['send']))
{

if(!empty($_POST['check_motorcycle'])) 
{
$selected_number = count($_POST['check_motorcycle']);

echo "You have selected the following ".$selected_number." motorcycle(s): <br/>";

foreach($_POST['check_motorcycle'] as $selected) 
{
echo "<p>".$selected ."</p>";
}
}
else{
echo "Select at least one option.";
}
}


?>

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

Result:




If you don't select any option



Result:


No comments:

Post a Comment

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