PDA

View Full Version : Forms question: How to make sure that the 2 last clicked values are stored


pvz
05-21-2004, 04:27 PM
Hello! I wonder if anyone knows how to populate 2 hidden fields with an onlick event, and to always make sure that the 2 last clicked values are in the fields?

It doesn't matter which value is in which field, but it always has to be only the 2 last clicked values that are stored...

Hope someone can help, here's something to (perhaps) give you a better idea of what i mean:

<input type="hidden" name="hidden1">
<input type="hidden" name="hidden2">

<a href="#" onlick="sendvalue(1)">send 1 to field!</a>
<a href="#" onlick="sendvalue(4)">send 4 to field!</a>
<a href="#" onlick="sendvalue(6)">send 6 to field!</a>
<a href="#" onlick="sendvalue(12)">send 12 to field!</a>

Thanks/Per

sad69
05-21-2004, 07:25 PM
This should do what you're looking for. Let me know if you require further explanation.


<script>
var write_to = 0;
function sendValue(val) {
document.getElementById("hidden"+write_to).value = val;
write_to = (write_to+1)%2; //changes from 0 to 1 to 0 to 1 to....
}
</script>

...

<input type="hidden" name="hidden1" id="hidden0">
<input type="hidden" name="hidden2" id="hidden1">

<a href="#" onlick="sendvalue(1)">send 1 to field!</a>
<a href="#" onlick="sendvalue(4)">send 4 to field!</a>
<a href="#" onlick="sendvalue(6)">send 6 to field!</a>
<a href="#" onlick="sendvalue(12)">send 12 to field!</a>


Sadiq.

pvz
05-21-2004, 11:15 PM
Many thanks for your reply! However, it didn't work. Below is the *exact* code which I tried, with "method='get'", so that I could check the querystring for values... But when I try it there are no values in the querystring.

Have I misunderstood? Again thanks for your reply, hope someone can help me if I have misunderstood

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
var write_to = 0;
function sendvalue(val) {
document.getElementById("hidden"+write_to).value = val;
write_to = (write_to+1)%2; //changes from 0 to 1 to 0 to 1 to....
}
</script>
</head>

<body>

<form action="switch.htm" method="get">
<input type="hidden" name="hidden1" id="hidden0">
<input type="hidden" name="hidden2" id="hidden1">
<input type="submit">
</form>
<a href="#" onlick="sendvalue(1)">send 1 to field!</a><br>
<a href="#" onlick="sendvalue(4)">send 4 to field!</a><br>
<a href="#" onlick="sendvalue(6)">send 6 to field!</a><br>
<a href="#" onlick="sendvalue(12)">send 12 to field!</a><br>


</body>
</html>

sad69
05-21-2004, 11:43 PM
Ok, this is the code I am using:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
var write_to = 0;
function sendvalue(val) {
document.getElementById("h"+write_to).value = val;
write_to = (write_to+1)%2; //changes from 0 to 1 to 0 to 1 to....
}
</script>
</head>

<body>

<form action="switch.htm" method="get">
<input type="hidden" name="hidden1" id="h0">
<input type="hidden" name="hidden2" id="h1">
<input type="submit">
</form>
<a href="javascript:sendvalue(1)">send 1 to field!</a><br>
<a href="javascript:sendvalue(4)">send 4 to field!</a><br>
<a href="javascript:sendvalue(6)">send 6 to field!</a><br>
<a href="javascript:sendvalue(12)">send 12 to field!</a><br>

</body>
</html>


Some comments:

I dunno what the href="#" does, but it wasn't calling sendvalue().. so I've changed it accordingly
NONE of your id's and your name's can be the same. This is why I changed the id from hidden to h. This is mainly a problem in IE. IE treats IDs and NAMEs the same.. don't ask me why!


Anyway, that works for me. Let me know if you have any further troubles.

Sadiq.

pvz
05-21-2004, 11:54 PM
Many thanks Sadiq, it works perfectly now! And also thanks for letting me know about IE and names/id's, that was something I didn't know!
/Per

Basscyst
05-22-2004, 12:01 AM
I dunno what the href="#" does, but it wasn't calling sendvalue().. so I've changed it accordingly

The pound (or hash) in the href just basically nulls the link. I think the real problem here was onlick="". Although that would be a pretty cool event handler.

Basscyst

sad69
05-22-2004, 12:05 AM
:eek: LOL :eek:

Oops, I didn't see that one at all!! Oh maaaaannn, well deserved long weekend coming to me! Good night every1!

Sadiq.