lpt.php
Código PHP:
Ver original<?php
if($_SERVER["REQUEST_METHOD"] === "POST") {
$binstr = implode("", array_map(function($b){ return $b === "true" ?
1 : 0; }, $_POST["bits"]));
// Aca envias el numero al puerto...
}
?>
<!DOCTYPE html>
<html>
<head>
<title>BITS LPT MANAGER</title>
<style>
table {
width: 400px;
border-collapse: collapse;
margin: 40px auto;
box-shadow: 0 0 5px #333;
border: 3px solid #333;
}
table tr th,
table tr td {
text-transform: uppercase;
text-align: center;
padding: 10px;
font-family: arial;
}
table tr td input {
width: 25px;
height: 25px;
}
</style>
</head>
<body>
<table>
<tr>
<th colspan="2">BINARIO:</th> <th colspan="2" id="binario"></th>
<th colspan="2">DECIMAL:</th> <th colspan="2" id="decimal"></th>
</tr>
<tr>
<th>8</th>
<th>7</th>
<th>6</th>
<th>5</th>
<th>4</th>
<th>3</th>
<th>2</th>
<th>1</th>
</tr>
<tr>
<td><input type="checkbox" id="bit8" value="8"></td>
<td><input type="checkbox" id="bit7" value="7"></td>
<td><input type="checkbox" id="bit6" value="6"></td>
<td><input type="checkbox" id="bit5" value="5"></td>
<td><input type="checkbox" id="bit4" value="4"></td>
<td><input type="checkbox" id="bit3" value="3"></td>
<td><input type="checkbox" id="bit2" value="2"></td>
<td><input type="checkbox" id="bit1" value="1"></td>
</tr>
</table>
<script>
function LPT(bits, monitor, delay) {
this.bits = bits;
this.monitor = monitor;
this.comet(delay);
}
LPT.prototype = {
"comet" : function(delay) {
var bits = new FormData();
this.bits.forEach(function(b) { bits.append("bits[]", b.checked); });
var request = new XMLHttpRequest();
request.open("post", "lpt.php");
request.onreadystatechange = function(request, delay) {
if(request.readyState === 4) {
var response = request.responseText,
success = false;
if(request.status == 200) {
try {
response = JSON.parse(response);
success = true;
} catch(e) { }
}
if(success) {
this.monitor.binario.innerHTML = response.binario;
this.monitor.decimal.innerHTML = response.decimal;
}
else
console.error(response);
setTimeout(this.comet.bind(this, delay), delay);
}
}.bind(this, request, delay);
request.send(bits);
}
}
new LPT([
document.getElementById("bit8"),
document.getElementById("bit7"),
document.getElementById("bit6"),
document.getElementById("bit5"),
document.getElementById("bit4"),
document.getElementById("bit3"),
document.getElementById("bit2"),
document.getElementById("bit1"),
], {
"binario" : document.getElementById("binario"),
"decimal" : document.getElementById("decimal"),
}, 1000);
</script>
</body>
</html>