PDA

View Full Version : location.href problem



EUDIS
Feb 25th, 2010, 04:19 PM
every time i try something it doesn't work for me :( god why is this javascript so hard to make it work... anyway, i just followed the guidelines to pass values from one page to another using ? and it simply, like always, DOES NOT work :(




<HEAD>

...
...

<script type="text/javascript">
<!--
function sendme(){
if (document.form1.question[0].checked == true) { whichone = 1; }
if (document.form1.question[1].checked == true) { whichone = 2; }
if (document.form1.question[2].checked == true) { whichone = 3; }
if (document.form1.question[3].checked == true) { whichone = 4; }
if (document.form1.question[4].checked == true) { whichone = 5; }
if (document.form1.question[5].checked == true) { whichone = 6; }
if (document.form1.question[6].checked == true) { whichone = 7; }
if (document.form1.question[7].checked == true) { whichone = 8; }
myString = qst1 + "=" + whichone;
location.href = "Poll_Jan2010_02.html" + '?' + myString;
}
//-->
</script>
</head>


<body>
<div align="center">
<p><img src="63/images/poll_logo.jpg" width="655" height="92">
</p>
<p>&nbsp;</p>
<h2><u>Question 1</u></h2>
<p><strong>Bla bla bla bla...</strong></p>
<form name="form1">
<table width="60%" border="0">
<tr>
<td><input name="question" type="radio" value="Grill">
option 1</td>
</tr>
<tr>
<td><input name="question" type="radio" value="Club">
option 2</td>
</tr>
<tr>
<td><input name="question" type="radio" value="Crepes">
option 3</td>
</tr>
<tr>
<td><input name="question" type="radio" value="Sandwiches">
option 4</td>
</tr>
<tr>
<td><input name="question" type="radio" value="Hotdog">
option 5</td>
</tr>
<tr>
<td><input name="question" type="radio" value="Special">
option 6</td>
</tr>
<tr>
<td><input name="question" type="radio" value="Refreshments">
option 7</td>
</tr>
<tr>
<td><input name="question" type="radio" value="Salads">
option 8</td>
</tr>
</table>
</form>
<p>
<a href="#null" class="style2" onclick="sendme();">Continue &gt;</a>
</p>
</div>
</body>

Philip M
Feb 25th, 2010, 04:35 PM
I think that qst1 is supposed to be a literal, not a variable, so

myString = "qst1=" + whichone;

Use an alert to check the value of myString.

glenngv
Feb 25th, 2010, 10:28 PM
You should have seen the url as something like Poll_Jan2010_02.html?undefined=1 and noticed that there is something wrong.

BTW, did you try my suggestion in this thread (http://www.codingforums.com/showthread.php?t=189969)?

Old Pedant
Feb 26th, 2010, 12:58 AM
The <!-- --> stuff is long obsolete. It was needed for MSIE version 3. 1997 or so.

And the code may work, but it seems kind of tedious.

How about:


function sendme()
{
var whichone = 0; // not answered?
for ( var i = 0; i <= 7; ++i )
{
if (document.form1.question[i].checked ) whichone = i+1;
}
// optional line:
if ( whichone == 0 ) { alert("you did not answer the question!"); return; }

location.href = "Poll_Jan2010_02.html?qst1=" + whichone;
}
</script>

glenngv
Feb 26th, 2010, 02:23 AM
It would be good to break out of the loop once the selected radio button is found.

for ( var i = 0; i < document.form1.question.length; ++i ){
if (document.form1.question[i].checked ) {
whichone = i+1;
break;
}
}

Old Pedant
Feb 26th, 2010, 02:45 AM
Yeah, I know. But with only a max of 8 iterations in the loop, it's not a big deal.

But if you are going to break, then maybe:


function sendme()
{
for ( var i = 0; i <= 7; ++i )
{
if (document.form1.question[i].checked ) break;
}
// optional line:
if ( i > 7 ) { alert("you did not answer the question!"); return; }

location.href = "Poll_Jan2010_02.html?qst1=" + (i+1);
}
</script>

SO many ways to excoriate a felinus domesticus.

EUDIS
Feb 26th, 2010, 09:23 AM
Thanks! The problem indeed was that qst1 is not a variable but a useable string. I liked the last method, it is fast short and also checks for non-answered questions :)

EUDIS
Feb 26th, 2010, 11:50 AM
Just one more help... I'm using unescape(location.search.substring(1)) to grab the text after ? in the url of the browser. In the last page, what i have (i mean the part after the ?) looks like:

qst1=7~qst2=1,1,1,5,1,1~qst3=2,4,3~qst4=6~qst5=5~qst6=1:Some%20piece%20of%20text

I think it would be ok to send this whole line to the perl and get it as it is in my mail, but i don't know maybe later i will realize that i need the information separately so if you could help me break down this to something like:
var qst1=7
var qst2=1,1,1,5,1,1
var qst3=2,4,3
var qst4=6
var qst5=5
var qst6=1:Some piece of text

Also, i noticed something strange.... When pressing "Next", and NOT always but most of the times (??!) browser moves on to the next page for a second, then goes back to the previous again and reloads it. I have to re-answer and re-hit "Next" 2,3 or 4 times until the next page loads and stays still :/ What the hell is that? :D