Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Conditional form submit, JavaScript

    Hi there,

    I am trying to generate a form for my website that enables users to order A4 photobooks. The idea is that the form should present the user with a few options and then, on submit, the user is taken to the relevant page on my shop-website, depending on the choices they have made.

    This webpage shows you the form as it is now with the code I have used so far.

    I have shown you the code below:

    First the html:

    Code:
    <form name="frm1" method="post" onSubmit="javascript:decide_action();" action="">
        <br>
        <input type="radio" name="group1" value="portrait"> Portrait<br>
        <input type="radio" name="group1" value="landscape" checked> 
    
    Landscape<br>
        <hr>
        <input type="radio" name="group2" value="layout1"> Layout 1<br>
        <input type="radio" name="group2" value="layout2"> Layout 2<br>
        <input type="radio" name="group2" value="layout3" checked> Layout 3<br>
        <input type="radio" name="group2" value="layout4"> Layout 4
        <hr>
        <input type="radio" name="group3" value="pagenos"> Page numbers<br>
        <input type="radio" name="group3" value="nopagenos" checked> No page numbers<br>
        <input type="submit" name="s1" value="Submit" />
        </form>
    Now the JavaScript as it is:

    Code:
     <script language="javascript">
    
    function decide_action()
    {
    if(check_buttons()==true)
    {
    if(document.frm1.group1[0].checked==true && document.frm1.group2[0].checked==true && document.frm1.group3[0].checked==true)
    {
    document.frm1.action="one.php";
    }
    else if(document.frm1.group1[0].checked==true && document.frm1.group2[0].checked==true && document.frm1.group3[1].checked==true)
    {
    document.frm1.action="two.php";
    }
    else if(document.frm1.group1[0].checked==true && document.frm1.group2[1].checked==true && document.frm1.group3[0].checked==true)
    {
    document.frm1.action="three.php";
    }
    else if(document.frm1.group1[0].checked==true && document.frm1.group2[1].checked==true && document.frm1.group3[1].checked==true)
    {
    document.frm1.action="four.php";
    }
    else if(document.frm1.group1[0].checked==true && document.frm1.group2[2].checked==true && document.frm1.group3[0].checked==true)
    {
    document.frm1.action="five.php";
    }
    else if(document.frm1.group1[0].checked==true && document.frm1.group2[2].checked==true && document.frm1.group3[1].checked==true)
    {
    document.frm1.action="six.php";
    }
    else if(document.frm1.group1[0].checked==true && document.frm1.group2[3].checked==true && document.frm1.group3[0].checked==true)
    {
    document.frm1.action="seven.php";
    }
    else if(document.frm1.group1[0].checked==true && document.frm1.group2[3].checked==true && document.frm1.group3[1].checked==true)
    {
    document.frm1.action="eight.php";
    }
    else if(document.frm1.group1[1].checked==true && document.frm1.group2[0].checked==true && document.frm1.group3[0].checked==true)
    {
    document.frm1.action="nine.php";
    }
    else if(document.frm1.group1[1].checked==true && document.frm1.group2[0].checked==true && document.frm1.group3[1].checked==true)
    {
    document.frm1.action="ten.php";
    }
    else if(document.frm1.group1[1].checked==true && document.frm1.group2[1].checked==true && document.frm1.group3[0].checked==true)
    {
    document.frm1.action="eleven.php";
    }
    else if(document.frm1.group1[1].checked==true && document.frm1.group2[1].checked==true && document.frm1.group3[1].checked==true)
    {
    document.frm1.action="twelve.php";
    }
    else if(document.frm1.group1[1].checked==true && document.frm1.group2[2].checked==true && document.frm1.group3[0].checked==true)
    {
    document.frm1.action="thirteen.php";
    }
    else if(document.frm1.group1[1].checked==true && document.frm1.group2[2].checked==true && document.frm1.group3[1].checked==true)
    {
    document.frm1.action="fourteen.php";
    }
    else if(document.frm1.group1[1].checked==true && document.frm1.group2[3].checked==true && document.frm1.group3[0].checked==true)
    {
    document.frm1.action="fifteen.php";
    }
    else if(document.frm1.group1[1].checked==true && document.frm1.group2[3].checked==true && document.frm1.group3[1].checked==true)
    {
    document.frm1.action="sixteen.php";
    }
    }
    
    function check_buttons()
    {
    var ok=false;
    for(i=0; i<3; i++)
    {
    if(document.frm1.ch[i].checked==true)
    {
    ok=true;
    }
    }
    if(ok==false)
    {
    alert("Select at least one option.");
    }
    return ok;
    }
    
    </script>
    As it doesn't work, I have clearly done something wrong. But, as I have just pieced the above together by having a look around the internet and don't know much about js code, I don't know what it is I have done wrong.

    Please could you have a look and try and correct my mistakes?

    Thanks in advance for your help.

    With kind regards,

    Rwthwyn

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    3,762
    Thanks
    23
    Thanked 548 Times in 547 Posts
    Because you have checked one button in each of your groups the check_buttons() function is not needed. So I would delete that and remove if(check_buttons()==true)
    { and the last }.

    In your else if's you are missing a document, this line:
    Code:
    	else if(document.frm1.group1[0].checked==true && document.frm1.group2[3].checked==true && document.frm1.group3[1].checked==true)
    	{
    		.frm1.action="eight.php";  // document needed at start of this line.
    Evolution - The non-random survival of random variants.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •