PDA

View Full Version : A Combination of 4 Check Boxes



PiperJim
11-30-2007, 09:58 PM
Hello to all you lovely programmers,who I hope will help me

I wish to set up a page with 4 check boxes so that you can select a combination of all 4 boxes A, B, C, D.
(ie) By selecting A you will go to a page title A
By selecting A,B you will go to a page title AB and so on
is this possible with javascript as I have been looking for a program for a few days now with no success

Thank PiperJim

zea726
12-04-2007, 07:21 AM
when you want to submit it you need to have the action value as variabel

<input type="hidden" id="hidden_id" value="">
<form id="form_id" method="post" action="<scritp>document.getElementById('hidden_id').value</script>">

..........
........
<input type="checkbox" id="c1">
<input type="checkbox" id="c2">
<input type="checkbox" id="c3">
<input type="checkbox" id="c4">

-----

<script>
var done =0;
if(document.getElementById("c1").checked){
done++}
else if(document.getElementById("c2").checked){
done++
}
.....
....
...
// now you need to change submit

function submit(){
if(done==1){
document.getElementById("hidden_id").value="http://dlk//";
form_id.submit();
</script>

<input type="button" onclick="submit()">

zea726
12-04-2007, 07:43 AM
function submit should be like this


function submit(){
if(done==1){
document.getElementById("hidden_id").value="http://dlk//";
form_id.submit();
}

else if(done==2){
document.getElementById("hidden_id").value="http://2.htm";
form_id.submit();
}
}

PiperJim
12-06-2007, 01:32 PM
Thank you for everything I have now got a program up and running Thanks again
Piperjim

Twey
12-06-2007, 06:27 PM
<script type="text/javascript">
function getValue(form, name) {
for(var i = 0, f = form.elements[name], n = f.length, r = []; i < n; ++i)
f[i].checked && r.push(f[i].value);
return r.length ? r : null;
}
</script>

<form action="redirector.php" onsubmit="this.action = (getValue(this, 'page') || ['error']).join('');">
<input type="checkbox" name="page" value="A">
<input type="checkbox" name="page" value="B">
<input type="checkbox" name="page" value="C">
<input type="checkbox" name="page" value="D">
</form>... where "redirector.php" is a page that will do the same thing for non-JS users.

Trinithis
12-07-2007, 12:28 AM
@Twey

Interesting idiom using

predicate && statement;
vs

if(predicate) statement;

f[i].checked && r.push(f[i].value);

Do you do it for reasons other than simply being fancy?

Does it also work in other languages, like C++ or Java?