/    Sign up×
Community /Pin to ProfileBookmark

Open 1 of 3 pages depending on form answers?

I’ve made a test of the “Most A answers? Then you’re type A” kind. Depending on the visitor’s answers I want my script to open one of three pages in a new window.

The problem is that it only opens one page (page c) no matter what I answer, and when my colleague tries it she only gets page a. I’ve tried clearing the cache and form memory as well as trying it in different browsers but it doesn’t help, and each radio button has the correct value (a, b or c). What am I doing wrong?

Thankful for advice!
/Cajsa

[code=php]<script language=”JavaScript” type=”text/javascript”>

<!– Begin
var done = new Array;
var ascore = 0;
var bscore = 0;
var cscore = 0;

function Engine(question, answer) {
if (!done[question]) {
done[question] = -1;
if (answer == “a”) ascore=ascore+1;
if (answer == “b”) bscore=bscore+1;
if (answer == “c”) cscore=cscore+1;
}
else {
alert(“Du har redan svarat!”);
}
}

function NextLevel () {
if (ascore >= bscore && ascore >= cscore){
window.open(“http://www.rfsu.se/test_sexuell_identitet_a.asp”,”mywindow”);
}
if (bscore >= ascore && bscore >= cscore){
window.open(“http://www.rfsu.se/test_sexuell_identitet_b.asp”,”mywindow”);
}
if (cscore >= ascore && cscore >= bscore){
window.open(“http://www.rfsu.se/test_sexuell_identitet_c.asp”,”mywindow”);
}
}

</script>[/code]

P.S. I call the script with this line of code:

[code=php]<INPUT TYPE=”button” VALUE=”Se resultat” onclick=”NextLevel ()”>[/code]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManSep 11.2007 — needs more source. i see that you have a lot of "ifs" but no else ifs. that means conditions can be met more than once, which i expect may impact the results you're getting.

needs more source showing how one gets to selecting answers to see how it will impact the final tally.
Copy linkTweet thisAlerts:
@CajsaauthorSep 12.2007 — Thanks for looking at the problem! Below is a part of the questionnaire. The total number of questions is 7, but I didn't include them here as they follow the same pattern.

I realize that a person might get as many a's as b's, which means that two of the script 'if' conditions will be met, leading to conflicting commands. I could fix that by creating three new result pages with "combination types" and rewrite the script so that for example if ascore == bscore, you are sent to the page combination_ab.asp.

But would the conflicting if's screw up the process even if the user gives straight a (or b or c) answers all the way through? This is what happened when I tried the test.

[code=php]
<form action="">

<div class="question">1. Question 1</div>
<INPUT TYPE="radio" VALUE="a" NAME="1"> Alternative a.<br>
<INPUT TYPE="radio" VALUE="b" NAME="1"> Alternative b.<br>
<INPUT TYPE="radio" VALUE="c" NAME="1"> Alternative c.<br>

<div class="question">2. Question 2</div>
<INPUT TYPE="radio" VALUE="a" NAME="2"> Alternative a.<br>
<INPUT TYPE="radio" VALUE="b" NAME="2"> Alternative b.<br>
<INPUT TYPE="radio" VALUE="c" NAME="2"> Alternative c.<br>

</form>
[/code]
Copy linkTweet thisAlerts:
@CajsaauthorSep 13.2007 — Hi Aaron,

Thanks for replying to my post "Open 1 of 3 pages depending on form answers?"! I've posted some more source code now, it would be really great if you could have a look at it and see what went wrong.

Thanks, Cajsa
Copy linkTweet thisAlerts:
@David_HarrisonSep 13.2007 — Post _all_ your code, or better yet, link to an online example or zip up your files and upload them as an attachment.
Copy linkTweet thisAlerts:
@CajsaauthorSep 14.2007 — Good idea. Here is the whole questions page.

/Cajsa

[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>

<head>

<title>Ottars stora hösttest: Hitta din sexualpolitiska identitet</title>
<meta name=Nyckelord content="test, sexuell identitet, Ottar Rapport, RFSU, Maria Lönn">
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=Description content="Testa dig själv: Hur ser du på identiteter? Gör Ottars test och ta reda på om du är queersnobben, bara människan, eller PK-helyllet.">
<meta name=robots content=all>

<link rel="stylesheet" href="stilmall.css" type="text/css">

<script language="JavaScript" type="text/javascript">

<!-- Begin
var done = new Array;
var ascore = 0;
var bscore = 0;
var cscore = 0;

function Engine(question, answer) {
if (!done[question]) {
done[question] = -1;
if (answer == "a") ascore=ascore+1;
if (answer == "b") bscore=bscore+1;
if (answer == "c") cscore=cscore+1;
}
else {

alert("Du har redan svarat!");
}
}

function NextLevel () {
if (ascore >= bscore && ascore >= cscore){
window.open("http://www.rfsu.se/test_sexuell_identitet_a.asp","mywindow");
}
if (bscore >= ascore && bscore >= cscore){
window.open("http://www.rfsu.se/test_sexuell_identitet_b.asp","mywindow");
}
if (cscore >= ascore && cscore >= bscore){
window.open("http://www.rfsu.se/test_sexuell_identitet_c.asp","mywindow");
}
if (ascore == bscore && ascore >= cscore){
window.open("http://www.rfsu.se/test_sexuell_identitet_ab.asp","mywindow");
}
if (ascore == cscore && ascore >= bscore){
window.open("http://www.rfsu.se/test_sexuell_identitet_ac.asp","mywindow");
}
if (bscore == cscore && bscore >= ascore){
window.open("http://www.rfsu.se/test_sexuell_identitet_bc.asp","mywindow");
}
}

</script>

</head>

<body>
<div id="frame">

<div id="content">

<h1>Definiera dig!</h1>
<h3>Hitta din sexualpolitiska identitet i Ottars stora hösttest</h3>

<br><img src="bilder/toppbild.jpg" alt="Illustration" class="toppbild"><br><br>

<p class="ingress">Intro.</p>
<p class="byline">Test: Maria Lönn<br>Illustration: Beata Boucht</p>


<form action="">
<div class="fraga">1. Question</div>
<INPUT TYPE="radio" VALUE="a" NAME="1"> Alternative a<br>
<INPUT TYPE="radio" VALUE="b" NAME="1"> Alternative b<br>
<INPUT TYPE="radio" VALUE="c" NAME="1"> Alternative c<br>

<div class="fraga">2. Question</div>
<INPUT TYPE="radio" VALUE="a" NAME="2"> Alternative a<br>
<INPUT TYPE="radio" VALUE="b" NAME="2"> Alternative b<br>
<INPUT TYPE="radio" VALUE="c" NAME="2"> Alternative c<br>

<div class="fraga">3. Question</div>
<INPUT TYPE="radio" VALUE="a" NAME="3"> Alternative a<br>
<INPUT TYPE="radio" VALUE="b" NAME="3"> Alternative b<br>
<INPUT TYPE="radio" VALUE="c" NAME="3"> Alternative c<br>

<div class="fraga">4. Question</div>
<INPUT TYPE="radio" VALUE="a" NAME="4"> Alternative a<br>
<INPUT TYPE="radio" VALUE="b" NAME="4"> Alternative b<br>
<INPUT TYPE="radio" VALUE="c" NAME="4"> Alternative c<br>

<div class="fraga">5. Question</div>
<INPUT TYPE="radio" VALUE="a" NAME="5"> Alternative a<br>
<INPUT TYPE="radio" VALUE="b" NAME="5"> Alternative b<br>
<INPUT TYPE="radio" VALUE="c" NAME="5"> Alternative c<br>

<div class="fraga">6. Question</div>
<INPUT TYPE="radio" VALUE="a" NAME="6"> Alternative a<br>
<INPUT TYPE="radio" VALUE="b" NAME="6"> Alternative b<br>
<INPUT TYPE="radio" VALUE="c" NAME="6"> Alternative c<br>

<div class="fraga">7. Question</div>
<INPUT TYPE="radio" VALUE="a" NAME="7"> Alternative a<br>
<INPUT TYPE="radio" VALUE="b" NAME="7"> Alternative b<br>
<INPUT TYPE="radio" VALUE="c" NAME="7"> Alternative c<br>
<br>

<p><b>Räkna nu ihop dina svar, och hitta din sexualpolitiska identitet!</b>
</p>

<INPUT TYPE="button" VALUE="See result" onclick="NextLevel ()">

</form>

</div>
</div>
</body>
</html>

[/code]
Copy linkTweet thisAlerts:
@David_HarrisonSep 14.2007 — OK, I've rewritten the JavaScript, about the only thing that remains is the if/else statements about where to redirect the user, though I did change the logic on that slightly to make it work as expected.

I've added two comments, one in the HTML at the beginning of the form, and another in the JavaScript towards the end. Please read them, they are important.

Something I notice about your original script, you never actually called your Engine function which seems like it should be the one to count up the scores, though I don't think it would work.

[upl-file uuid=3a2fbcf4-374e-428a-9290-cde71bfaf9e7 size=2kB]Cajsa.zip[/upl-file]
Copy linkTweet thisAlerts:
@CajsaauthorSep 26.2007 — Thank you so much for helping me out, I really didn't know how to solve the problem. It's truly great that people like you take the time to give assistance.

/Cajsa
×

Success!

Help @Cajsa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...