Website Development Prices

Search Blog

Thursday, June 16, 2016

Preuzimanje podataka iz radio dugmadi (Retrieving data from radio buttons)

Ako koristite radio dugmad, korisnik u jednom trenutku moze da izabere samo jedno od njih, sto je korisno ako korisniku zelite da prikazete opcije koje se iskljucuju.

Primer: za grupisanje radio dugmadi odaberite isti naziv za dva ili vise njih.

HTML kod

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



<body>

<h2>Upotreba radio dugmadi</h2>
<form action="radio-dugmad.php" method="post">
<h3>Sta vise volite?</h3>
<input name="vozilo" type="radio" value="Motocikl">Motocikl<br />
<input name="vozilo" type="radio" value="Automobil">Automobil<br /><br />
<input name="posaljite" type="submit"  Value="Posaljite"/>
</form>

</body>

</html>

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

Rezultat:




Ako zelite da otkrijete koje je radio dugme izabrano, u nizu $_POST cete koristiti naziv grupe.

PHP kod

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

<body>
<h1>Preuzimanje podataka iz radio dugmadi</h1>

<?php
echo "Izabrali ste ", $_POST["vozilo"];
?>

</body>

</html>

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

Rezultat:




Ako zelite vise skupova radio dugmadi, svakoj grupi dugmadi dajte razlicito ime atributa name.

Primer 2:

HTML kod

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

</head>
<body>

<h2>Upotreba radio dugmadi</h2>
<form action="radio-dugmad-2.php" method="post">
<h3>Sta vise volite?</h3>
<input name="vozilo1" type="radio" value="Suzuki GSX-R 750">Suzuki GSX-R 750<br />
<input name="vozilo1" type="radio" value="2004 KTM 250 EXC Racing">2004 KTM 250 EXC Racing<br /><br />

<input name="vozilo2" type="radio" value="Toyota FJ Cruiser">Toyota FJ Cruiser<br />
<input name="vozilo2" type="radio" value="Suzuki Kizashi">Suzuki Kizashi<br /><br />

<input name="posaljite" type="submit"  Value="Posaljite"/>
</form>

</body>

</html>

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

Rezultat:




PHP kod

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

<body>
<h1>Preuzimanje podataka iz radio dugmadi</h1>

Izabrali ste

<?php
if (isset($_POST["vozilo1"]))
echo $_POST["vozilo1"], " i ";
if (isset($_POST["vozilo2"]))
echo $_POST["vozilo2"], "<br />";
?>

</body>

</html>

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

Rezultat:




If you use radio buttons, a user at a time can choose only one of them, which is useful if you want to display to a user options that are exclusive.

Example: to group radio buttons select the same name for two or more of them.

HTML code

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

<body>
<h1>Using radio buttons</h1>

<form action="radio-buttons.php" method="post">
<h3>What do you prefer?</h3>
<input name="vehicle" type="radio" value="Motorcycle">Motorcycle<br />
<input name="vehicle" type="radio" value="Car">Car<br /><br />
<input name="send" type="submit"  Value="Send"/>
</form>
</body>

</html>

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

Result:


If you want to discover what radio button is selected, in array $_POST you will use the group's name.

PHP code

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

<body>
<h1>Retrieving data from radio buttons</h1>

<?php
echo "You chose ", $_POST["vehicle"];
?>

</body>

</html>

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

Result:


If you want more sets of radio buttons, to each group of buttons give different name using attribute name.

Example 2:

HTML code

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

</head>
<body>

<h2>Using radio buttons</h2>
<form action="radio-buttons-2.php" method="post">
<h3>What do you prefer?</h3>
<input name="vehicle1" type="radio" value="Suzuki GSX-R 750">Suzuki GSX-R 750<br />
<input name="vehicle1" type="radio" value="2004 KTM 250 EXC Racing">2004 KTM 250 EXC Racing<br /><br />

<input name="vehicle2" type="radio" value="Toyota FJ Cruiser">Toyota FJ Cruiser<br />
<input name="vehicle2" type="radio" value="Suzuki Kizashi">Suzuki Kizashi<br /><br />

<input name="send" type="submit"  Value="Send"/>
</form>

</body>

</html>

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

Result:



PHP code

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

<body>
<h1>Retrieving data from radio buttons</h1>

You chose

<?php
if (isset($_POST["vehicle1"]))
echo $_POST["vehicle1"], " and ";
if (isset($_POST["vehicle2"]))
echo $_POST["vehicle2"], "<br />";
?>

</body>

</html>

Save this file as radio-buttons-2.php, in the same folder as the radio-buttons-2.html.

Result:



No comments:

Post a Comment

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