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 7 of 7

Thread: Help

  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post Help

    Alright what I'm trying to do is create a check box type deal in which you choose what site you want to choose to go to by the choices you make and I created a sample code that is coming out with a error. can someone fix it and tell me what I did wrong thanks.(p.s its a sample cause it only dealt with 2/9 of the ways the final answer would turn out)
    Code:
    <html>
    <head>
    <title> New Script </title>
    <script type="text/javascript">
    
    function  {
    if (a=1) (b=0) (c=0) 
    then
    {
    x=(www.wix.com)
    }
    function {
    if (a=0) (b=0) (c=0)
    then
    {
    x=(www.google.com)"
    }
    </script>
    </head>
    <body>
    
     <input type="checkbox" name="CBox0" 
     onsubmit="if(this.checked){var a=1} else {var a=0}"> Chat Room
    
     <br>
    
    <input type="checkbox" name="CBox1" 
      onsubmit="if(this.checked){var b=1} else {var b=0}"> Music
    
      <br>
    
    <input type="checkbox" name="CBox2"  
      onsubmit="if(this.checked){var c=1} else {var c=0}"> Games
      
      <br>
     
     <input type="submit" name="submit">
      </body>
    </html>
    Last edited by VIPStephan; 09-26-2012 at 10:15 AM. Reason: removed link & added code BB tags

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    This isn't even vaguely correct:
    Code:
    if (a=1) (b=0) (c=0)
    Where did you dream up that syntax??

    First of all, you are only allowed one ( ) as the container for the condition of an if statement.

    And then you are using =. In JavaScript (and in Java and C and C++ and C# and PHP and ...) the = operator is *ALWAYS* an assignment! Yes, even in an if statement.

    To TEST a value, you must use ==.

    So, I *THINK* what you are after is this:
    Code:
    if (a==1 && b==0 && c==0)
    But there are so many other things wrong with that code, it's tough to know where to start.

    A function must have a name and the name must have at least ( ) after it [optionally, parameter names can appear between the parentheses].

    There is no such thing as onsubmit for checkboxes.

    This isn't vaguely right:
    Code:
    x=(www.wix.com)
    and I'm not even sure what you expected it to do.

    You have nothing at all calling your (illegal) functions.

    There is no such thing as a then keyword in JavaScript (or in Java or C++ or ... ).
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Jeremyfto (09-26-2012)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Jeremy fto - I am sorry to say that your code is riddled with errors. It is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase.

    You would do best to discuss your problems with your teacher.

    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.
    Last edited by Philip M; 09-26-2012 at 08:22 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I see what you mean by the a==. you see I have never actully gone to a class on coding iv just watched tutorials. I was building this for a bigger project. its spost to be multi-choice check boxes but I'm guessing I failed at it. any tips on where I should be headed to get this coded?
    Quote Originally Posted by Old Pedant View Post
    This isn't even vaguely correct:
    Code:
    if (a=1) (b=0) (c=0)
    Where did you dream up that syntax??

    First of all, you are only allowed one ( ) as the container for the condition of an if statement.

    And then you are using =. In JavaScript (and in Java and C and C++ and C# and PHP and ...) the = operator is *ALWAYS* an assignment! Yes, even in an if statement.

    To TEST a value, you must use ==.

    So, I *THINK* what you are after is this:
    Code:
    if (a==1 && b==0 && c==0)
    But there are so many other things wrong with that code, it's tough to know where to start.

    A function must have a name and the name must have at least ( ) after it [optionally, parameter names can appear between the parentheses].

    There is no such thing as onsubmit for checkboxes.

    This isn't vaguely right:
    Code:
    x=(www.wix.com)
    and I'm not even sure what you expected it to do.

    You have nothing at all calling your (illegal) functions.

    There is no such thing as a then keyword in JavaScript (or in Java or C++ or ... ).

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,928
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Jeremyfto View Post
    I see what you mean by the a==. you see I have never actully gone to a class on coding iv just watched tutorials. I was building this for a bigger project. its spost to be multi-choice check boxes but I'm guessing I failed at it. any tips on where I should be headed to get this coded?
    Do what you would do if your car or a domestic appliance broke down or you had toothache - pay a professional who understands what they are doing to fix it. You can post in the Web Projects section of his forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,211
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    I may regret this, but...
    Code:
    <!DOCTYPE html5>
    <html>
    <body>
    <form id="theForm">
    <label><input type="checkbox" name="choice" value="1"/> Chat Room</label><br/>
    <label><input type="checkbox" name="choice" value="2"/> Music</label><br/>
    <label><input type="checkbox" name="choice" value="4"/> Games</label><br/>
    <input name="doit" type="button" value="Find Site"/>
    </form>
    <script type="text/javascript">
    (
      function( )
      {
          // replace my dummy urls with the real ones for the 7 possible combinations
          var targets = [
              null, /* no boxes checked */
              "chat.com", 
              "music.com",
              "chat_music.com",
              "games.com",
              "chat_games.com",
              "music_games.com",
              "chat_music_games.com"
          ];
          
          // attach event to the button
          form = document.getElementById("theForm");
          
          form.doit.onclick = function( )
          {
              var cbs = form.choice;
              var choiceSum = 0;
              for ( var c = 0; c < cbs.length; ++c )
              {
                  if ( cbs[c].checked ) choiceSum += Number(cbs[c].value);
              }
              if ( choiceSum != 0 )
              {
                  location.href = "http://www." + targets[choiceSum];
              }
          }
      }
    )( );
    </script>
    </body>
    </html>
    A completely different approach than your complex set of "ifs".

    By giving a "weight" to the 3 choices, you simply take the sum of the weights (which will be an integer from 0 to 7) and use it to pick the URL from an array of URLs.

    Here, I have assumed that if the user checks no checkboxes, then you do not send her/him anywhere. But you can easily change that, of course.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    Jeremyfto (09-27-2012)

  • #7
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thank you so much. I appreciate your help .
    Quote Originally Posted by Old Pedant View Post
    I may regret this, but...
    Code:
    <!DOCTYPE html5>
    <html>
    <body>
    <form id="theForm">
    <label><input type="checkbox" name="choice" value="1"/> Chat Room</label><br/>
    <label><input type="checkbox" name="choice" value="2"/> Music</label><br/>
    <label><input type="checkbox" name="choice" value="4"/> Games</label><br/>
    <input name="doit" type="button" value="Find Site"/>
    </form>
    <script type="text/javascript">
    (
      function( )
      {
          // replace my dummy urls with the real ones for the 7 possible combinations
          var targets = [
              null, /* no boxes checked */
              "chat.com", 
              "music.com",
              "chat_music.com",
              "games.com",
              "chat_games.com",
              "music_games.com",
              "chat_music_games.com"
          ];
          
          // attach event to the button
          form = document.getElementById("theForm");
          
          form.doit.onclick = function( )
          {
              var cbs = form.choice;
              var choiceSum = 0;
              for ( var c = 0; c < cbs.length; ++c )
              {
                  if ( cbs[c].checked ) choiceSum += Number(cbs[c].value);
              }
              if ( choiceSum != 0 )
              {
                  location.href = "http://www." + targets[choiceSum];
              }
          }
      }
    )( );
    </script>
    </body>
    </html>
    A completely different approach than your complex set of "ifs".

    By giving a "weight" to the 3 choices, you simply take the sum of the weights (which will be an integer from 0 to 7) and use it to pick the URL from an array of URLs.

    Here, I have assumed that if the user checks no checkboxes, then you do not send her/him anywhere. But you can easily change that, of course.


  •  

    Tags for this Thread

    Posting Permissions

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