I want to send information about a set of 8 radiobuttons to a device
that listens to TCP/IP on port 80.
I have 8 sets of radiobuttons, each has a on and off state.
When this form is send, I would like that the state of the buttons is
converted to a byte.
So button the on state of these buttons adds the following value to
the value that is send in the GET command.
Button 1 = 1
Button 2 = 2
Button 3 = 4
Button 4 = 8
Button 5 = 16
Button 6 = 32
Button 7 = 64
Button 8 = 128
So if button 1 and 8 are marked at the time the form is send, the
script adds the values of these buttons 1 + 128 = 129 and send this as
result of the form.
Can I do this easily?
>I have 8 sets of radiobuttons, each has a on and off state.
>When this form is send, I would like that the state of the buttons is
>converted to a byte.
>Can I do this easily?
Apparently not. But I can : consider
<form> DEMO ONLY; lacks polish
<br>..0..1
<br><input type=radio name=x1><input type=radio name=x1> 1
<br><input type=radio name=x2><input type=radio name=x2> 2
<br><input type=radio name=x3><input type=radio name=x3> 4
<br><input type=radio name=x4><input type=radio name=x4> 8
<br><input type=radio name=x5><input type=radio name=x5> 16
<br><input type=radio name=x6><input type=radio name=x6> 32
<br><input type=radio name=x7><input type=radio name=x7> 64
<br><input type=radio name=x8><input type=radio name=x8> 128
<br> <input type=button value="?" onClick="Wot(this.form)">
<script>
function Wot(F) { var X=0, J=9
while (--J) X = X*2 + F["x"+J][1].checked
alert(X) }
</script>
There, F["x"+J] refers to a radiobutton pair.
It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Here's something to start playing with:
-Copy paste it and edit the line deviceURL= "http://0.0.0.0/headers?"
to point to the ip of your device.
-save it as utf8.
-name it whatever.html.
-open it in the browser (better not IE).
-click the bits buttons to toggle them between 0/1
-click send and an http GET request will be sent to your device:
something like :
http://0.0.0.0/headers?&rnd=6808913279445241&data=fe
-in the device, extract from the url the "&data=xx" part and ignore
the rest.
-the bits are sent in text as hex: the last 2 chars of the url.
-feel free to ask any questions.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Binary & buttons</title>
<meta name="generator" content="TextMate http://macromates.com/">
<meta name="author" content="jorge">
<!-- Date: 2008-10-19 -->
<style>
button { width: 50px; height: 35px; }
.sendBtn { width: 90px; height: 35px; }
</style>
<script>
window.onload= function (event) {
var i= 0, max= 7, d= document, bits= [], e,
sendBtn= d.createElement('button');
do {
e= bits[i]= d.createElement('button');
e.bit= 1;
e.weight= Math.pow(2, max-i);
(e.onclick= bitClick).apply(e);
d.body.appendChild(e);
d.body.appendChild(d.createTextNode(i<max ? "+" : "="));
} while (++i <= max)
d.body.appendChild(sendBtn);
sendBtn.className= "sendBtn";
sendBtn.onclick= function () {
var e, deviceURL= "http://0.0.0.0/headers?",
rnd= "&rnd="+ (""+Math.random()).substr(2);
e= d.createElement('img');
e.src= deviceURL+ rnd+ "&data="+ sendBtn.hex;
d.body.appendChild(d.createTextNode(e.src));
d.body.appendChild(e);
d.body.appendChild(d.createElement('br'));
};
d.body.appendChild(d.createElement('br'));
function bitClick (event) {
var i= bits.length, hex= 0;
this.bit= +(!this.bit);
this.innerHTML= this.bit*this.weight;
while (--i >= 0) {
hex+= +bits[i].innerHTML;
}
if ((sendBtn.hex= hex.toString(16)).length === 1) {
sendBtn.hex= "0"+ sendBtn.hex;
}
sendBtn.innerHTML= "send: 0x"+sendBtn.hex;
}
};
</script>
</head>
<body>
</body>
</html>
--
Jorge
"Silly things such as logic, common sense and rational thoughts are
not allowed in this forum."