/    Sign up×
Community /Pin to ProfileBookmark

Using IF/ELSE statements to link forms on separate pages

Hi all

I have a form which contains visitor details, one of which is a drop down box for “marital status”.

I then have a form on a different page that contains some of their home details which is linked via a “next” A HREF command.

Is it possible to use an IF/ELSE statement so that when selecting next with “Married” selected instead of jumping to house details it goes to partners details (another form on another page)?

If so, how? (what would the code be?)

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@peteybauthorAug 02.2005 — anyone?????
Copy linkTweet thisAlerts:
@artoAug 02.2005 — This should do the trick:
[CODE]
<a href="#" onclick="
var form=document.forms['form'];
var select=form.elements['marital'];
if (select.options[select.selectedIndex].value=='married')
window.location='partner.html';
else
window.location='home.html';
return false;
">Next</a>
[/CODE]

However, clicking this link won't submit the form, so all data will be lost.

Maybe you want to change form's destination and submit it when clicking the link? If so, try this:
[CODE]
<a href="#" onclick="
var form=document.forms['form'];
var select=form.elements['marital'];
if (select.options[select.selectedIndex].value=='married')
form.setAttribute('action','partner.html');
else
form.setAttribute('action','home.html');
form.submit();
return false;
">Next</a>
[/CODE]

Hope it helps.

Arto
Copy linkTweet thisAlerts:
@peteybauthorAug 02.2005 — 
  • 1. If i wanted to submit all the details from "ydetails.html" and "partner.html" and all my other pages on a final submit (not on either of those stated) this code would eliminate this option?


  • 2. the code didnt work and said there was an error with the "elements" bit



  • 3. the page i am on is "ydetails.html", the page the Next button should go to if Co-Inhabiting or Married is selected is "partner.html" and the page if not selected is "bdetail.html" - could you write more specific code with this info?
  • Copy linkTweet thisAlerts:
    @artoAug 02.2005 — 
    1. If i wanted to submit all the details from "ydetails.html" and "partner.html" and all my other pages on a final submit (not on either of those stated) this code would eliminate this option?
    [/QUOTE]

    You can't have one final submit for few forms on different pages. You have to submit each one, parse entered data with some server-side code and then display next form. This way you accumulate data user is entering page after page.

    But maybe I don't understand what are you trying to do. Some bits of your code could be helpful.


    2. the code didnt work and said there was an error with the "elements" bit
    [/QUOTE]

    Did you remember to change form and select names to ones you're using? All values in ' ' are just examples.

    And what browser are you using?


    3. the page i am on is "ydetails.html", the page the Next button should go to if Co-Inhabiting or Married is selected is "partner.html" and the page if not selected is "bdetail.html" - could you write more specific code with this info?
    [/QUOTE]

    [CODE]
    <a href="#" onclick="
    var form=document.forms['form'];
    var select=form.elements['marital'];
    if ( (select.options[select.selectedIndex].value=='Co-Inhabiting') ||
    (select.options[select.selectedIndex].value=='Married') )
    form.setAttribute('action','partner.html');
    else
    form.setAttribute('action','bdetail.html');
    form.submit();
    return false;
    ">Next</a>
    [/CODE]

    But you still have to adjust names of form and select in second and third line.

    Arto
    Copy linkTweet thisAlerts:
    @peteybauthorAug 05.2005 — 
  • 1. you said "You submit each one" - whilst this might be true, does the visitor only press submit once i.e. the "next" buttons are hidden submits and the final "submit quote" button is on the last page?


  • 2. how do i parse entered data?


  • 3. "name of form and select" need to be changed, do you mean the VAR names? or the 'text' in quotes?
  • Copy linkTweet thisAlerts:
    @peteybauthorAug 05.2005 — with reference to question 3 -

    the name of my form is "partner" so have changed the ['form'] to ['partner'] and the name of the marital status is ['customer_status'] so have changed accordingly.

    whilst the Next buttoin now takes me to the "bdetail.html" instead of an error, this uis the only page it takes me to, as in not to "partner.html" when married/cohabiting selected.
    Copy linkTweet thisAlerts:
    @artoAug 05.2005 — you said "You submit each one" - whilst this might be true, does the visitor only press submit once i.e. the "next" buttons are hidden submits and the final "submit quote" button is on the last page?[/QUOTE]It doesn't matter if submits are hidden as next links or not. They must be submits, but how you make them look is up to you.

    how do i parse entered data?[/QUOTE]This is not javascript question. Like I told you, you must use some server-side language (well, server-side javascript does exist, but it's not common). For example, in PHP you use $_POST array to get values from form.

    "name of form and select" need to be changed, do you mean the VAR names? or the 'text' in quotes?[/QUOTE]Yes, I mean text in quotes:

    var form=document.forms['INSERT YOUR FORM NAME HERE'];

    the name of my form is "partner" so have changed the ['form'] to ['partner'] and the name of the marital status is ['customer_status'] so have changed accordingly.

    whilst the Next buttoin now takes me to the "bdetail.html" instead of an error, this uis the only page it takes me to, as in not to "partner.html" when married/cohabiting selected.[/QUOTE]
    Well, I can't say anything about that without seeing the code. Maybe values in select options aren't 'Co-Inhabiting' and 'Married' but something else?

    Arto
    Copy linkTweet thisAlerts:
    @peteybauthorAug 05.2005 — ive attached my code, i have a thread in PHP trying to help me discover how to link all the forms by $_POST array but its not going too well.

    [upl-file uuid=07a7fad9-1423-4050-8eaa-f455445c37b9 size=12kB]ydetail.txt[/upl-file]

    [upl-file uuid=f06fdf7c-07f8-4149-92d8-0d495d6b9b2e size=10kB]partner.txt[/upl-file]

    [upl-file uuid=d79462c6-5c20-4c33-bac4-cd92f3d17114 size=15kB]bdetail.txt[/upl-file]
    Copy linkTweet thisAlerts:
    @artoAug 05.2005 — It didn't work because you need to give values to options. Change options like this:[CODE]<option value="Married">Married</option>
    <option value="Single" selected>Single</option>
    <option value="Co-Habiting">Co-Habiting</option>
    <option value="Widowed">Widowed</option>
    <option value="Divorced">Divorced</option>[/CODE]

    Arto
    Copy linkTweet thisAlerts:
    @peteybauthorAug 05.2005 — cool, it works, its alive!!!!!!!!!!!!

    i have a similar problem but i think it might need php.

    i have two different calculators that work out premiums with different rates. The calculator used is dependent on whether "Standard" cover or "Accidental Damage" cover is chosen.

    The problem is that the calculator is not the next link in the process.

    Is there a way to store the cover chosen until the page one before the calculator?
    Copy linkTweet thisAlerts:
    @artoAug 05.2005 — Yes, using PHP you can store it in session for as long as you need.

    Arto
    Copy linkTweet thisAlerts:
    @peteybauthorAug 05.2005 — i know this isnt a php thread but do you know how?
    Copy linkTweet thisAlerts:
    @peteybauthorAug 05.2005 — its really wierd, when i use the next button (married or single) in my documents it works but when i put it onto my website on the server it doesnt???
    Copy linkTweet thisAlerts:
    @peteybauthorAug 05.2005 — sorted it, soz. had action="bdetail.php" action="post" in the form tag, trying to work out how to connect multiple forms from dif pages to a final submit button and left some code in by accident. oops
    ×

    Success!

    Help @peteyb 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.17,
    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,
    )...