HTML
<select onchange="postit(event)">
<option class="menuoption2" value="gold">Gold</option>
<option class="menuoption2" value="silver">Silver</option>
<option class="menuoption2" value="bronze">Bronze</option>
</select>
JS / AJAX
Posts the value to update.php where metal=value
<script type="text/javascript">//<![CDATA[
var xmlhttp;
function postit(event){
var value = event.target.value;
xmlhttp=null;
var Url="update.php"
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null) {
xmlhttp.onreadystatechange=getResponse;
xmlhttp.open("POST", Url, true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send( "metal=" + value);
} else {
alert("UNEXPECTED ERROR: XMLHttpRequest not supported");
}
}
function getResponse(){
if (xmlhttp.readyState==4){
if (xmlhttp.status==200){
var json = JSON.parse(xmlhttp.responseText);
}
else{
alert("UNEXPECTED AJAX ERROR: : " + xmlhttp.statusText + "HTTP Status: " + xmlhttp.status);
}
}
}
//]]>
</script>
PHP
<?php
$metal = $_POST['metal'];
// This is just to validate the received data,
// mostly needed if you are storing in a database.
$metals = array('gold','silver','bronze');
$status = 'fail';
if($metals[$metal] != null){
$status = 'pass';
//store $metal
}
//To return a value to JS after the update, add the code below
$return['status'] = $status;
header(header('Content-Type: text/json; charset=utf-8');
echo json_encode($return);
?>
No Return Value to JS
Remove these two lines from the JS
var Url="update.php"
xmlhttp.onreadystatechange=getResponse;
Remove the function getResponse()
move global var xmlhttp;
into the postit() function as a local var:
From
var xmlhttp;
function postit(){
To
function postit(){
var xmlhttp;