Website Development Prices

Search Blog

Sunday, July 10, 2016

Upotreba dugmadi sa JavaScript-om i skrivenom kontrolom (Using buttons with JavaScript and hidden control)

Dugmad se razlikuju od ostalih kontrola, posto kada ih kliknete, ne ostaju kliknuta, sto znaci da se informacija o tome ne salje sa formom. 

Ako zelite da u PHP-u odredite koje je dugme korisnik kliknuo, jedan nacin je da pomesate JavaScript i skrivenu kontrolu.

Primer: kada korisnik klikne dugme, JavaScript iz primera smesta ime dugmeta u skrivenu kontrolu pod imenom "dugme", a onda se uz pomoc funkcije submit rezultat salje do skripta php-dugmad-1.php.

HTML kod

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


<body>
<h1>Upotreba dugmadi 1</h1>

<h3>Upotreba dugmadi sa JavaScript-om i skrivenom kontrolom</h3>

<form name="forma1" action="php-dugmad-1.php" method="post">
<input type="hidden" name="dugme">
<input type="button" value="Dugme 1" onclick="dugme1()">
<input type="button" value="Dugme 2" onclick="dugme2()">
<input type="button" value="Dugme 3" onclick="dugme3()">
</form>

<script script="JavaScript">

function dugme1()
{
document.forma1.dugme.value = "dugme 1"
forma1.submit()
}

function dugme2()
{
document.forma1.dugme.value = "dugme 2"
forma1.submit()
}

function dugme3()
{
document.forma1.dugme.value = "dugme 3"
forma1.submit()
}

</script>

</body>

</html>

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

PHP kod

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

<body>
<h1>Citanje iz dugmadi 1</h1>
Kliknuli ste na

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

</body>

</html>

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

Rezultat:






Buttons are different from other controls, since when you click them, they don't remain clickable, meaning that information about it is not sent with the form.

If you want to determine in PHP which button user clicked, one way is to mix the JavaScript and hidden control.

Example: when the user clicks the button, JavaScript places name of button in the hidden control called "mybutton", and then with the help of function submit the result is sent to the script php-button-1.php.

HTML code

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

<body>
<h1>Using buttons 1</h1>

<h3>Using buttons with JavaScript and hidden control</h3>

<form name="form1" action="php-buttons-1.php" method="post">
<input type="hidden" name="mybutton">
<input type="button" value="Button 1" onclick="button1()">
<input type="button" value="Button 2" onclick="button2()">
<input type="button" value="Button 3" onclick="button3()">
</form>

<script script="JavaScript">

function button1()
{
document.form1.mybutton.value = "button 1"
form1.submit()
}

function button2()
{
document.form1.mybutton.value = "button 2"
form1.submit()
}

function button3()
{
document.form1.mybutton.value = "button 3"
form1.submit()
}

</script>

</body>

</html>

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

PHP code

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

<body>
<h1>Reading from buttons 1</h1>
You clicked on

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

</body>

</html>

Save this file as php-buttons-1.php in the same folder as the php-buttons-1.html.


Result:





No comments:

Post a Comment

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