File HTML:
Codice: Seleziona tutto
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>The most useful button on the internet</title>
<script src="button.js" type="text/javascript"></script>
<link href="buttonStyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<h2>The most useful button on the internet</h2>
<h3>So far this button has been pressed</h3>
<h1 id="number"></h1>
<h3>times</h3>
<div>
<button id="theButton" onclick="buttonThing()"></button>
</div>
</body>
</html>
Codice: Seleziona tutto
document.addEventListener("DOMContentLoaded", function()
{
console.log(Caricata la pagina. Richiesta inviata");
fetchValue();
}
);
function fetchValue()
{
var req = new XMLHttpRequest;
req.onreadystatechange = function ()
{
if ( req.readyState == 4 && req.status == 200 )
document.getElementById("number").innerHTML = req.responseText;
}
req.open("GET", "http://robapertestare.altervista.org/TheButton/button.php?use=0", true);
req.send();
}
function buttonThing()
{
var req = new XMLHttpRequest;
req.onreadystatechange = function ()
{
if ( req.readyState == 4 && req.status == 200 )
{
console.log("Aggiunto 1!");
fetchValue();
}
}
req.open("GET", "http://robapertestare.altervista.org/TheButton/button.php?use=1", true);
req.send();
}
Codice: Seleziona tutto
?php
//Non ho cancellato i campi per sicurezza, sono proprio vuoti. Altervista fa fare il login in automatico
$server = "";
$username = "";
$password = "";
$dbname = "my_robapertestare";
$connection = new mysqli($server, $username, $password, $dbname);
if ( $connection->connect_error )
die("Connessione fallita:". $connection->connect_error );
//Esegui query(aggiunge 1) se "use" nel URL è a 1
if ( $_GET["use"] == "1")
{
$sql = "UPDATE TheButton SET Premuto = Premuto+1 WHERE 1";
echo $connection->query($sql) or die("Query di aumento fallita:". $connection->error);
}
else
{
$sql = "SELECT Premuto FROM `TheButton` WHERE 1";
echo $connection->query($sql) or die("Query di fetch fallita:". $connection->error);
}
?>
http://robapertestare.altervista.org/Th ... utton.html