her er et lille eks hurtigt flækket sammen
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.button-standard{
width:100px;
text-align: center;
}
</style>
<script type="text/javascript">
function UpdateGoal(elm){
var elmgoals = document.getElementById("goals");
var url = "request.php";
var params = "id="+elm.value+"&goals="+elmgoals.value+"&rand="+Math.random();
var http = new XMLHttpRequest(); // gider ikke teste om browseren understøtter XMLHttpRequest()
http.open("POST",url ,true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.send(params);
http.onreadystatechange = function () {
if(http.readyState == 4 && http.status == 200 ){
elmgoals.value = http.responseText;
}
}
}
</script>
</head>
<body>
<?php
$p_g[goal] = 0;
?>
<form action="?page=play" method="post">
Goals:
<input type="submit" class="grey button-standard" id="goals" value="<?php echo $p_g[goal]; ?>" disabled />
<input type="hidden" name="type" value="goals" />
<input type="button" name="score[]" class="green" value="+1" onclick = "UpdateGoal(this)" />
<input type="button" name="score[]" class="yellow" value="-1" onclick = "UpdateGoal(this)" />
<input type="button" name="score[]" class="red" value="0" onclick = "UpdateGoal(this)" />
</form>
</body>
</html>
request.php
<?php
$id = $_POST['id'];
$goals = $_POST['goals'];
// her skal db bare opdateres, og ny værdi echo'es ud
switch($id){
case 0: $goals = 0; break;
case -1: $goals -= 1; break;
case 1: $goals += 1; break;
}
echo $goals;
?>
jeg bruger ikke <form...>, så den ville kunne slettes, men bruges sikkert i anden sammenhæng