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
  1. #1
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    multiple 'if' statements - please help a newbie!

    Hey dudes!

    Sorry to ask such a simple question but I have a time limit and need to know the answer quickly! My mate suggested a quick post on here!

    I am working on a project for college - whilst i'm not studying Javascript I need to add some for functionality of my website. I know a bit to get by but seem to have got this wrong all day! I'll simplify it .....

    I know that if I write .......

    Code:
    var userAnswer = prompt("Are you feeling OK today?");
    if (userAnswer === "Yes")
    {
    	document.write("Great - glad to hear it");
    }
    else
    {
    	document.write("Oh dear! That is a shame!")
    }

    ....... a prompt box appears asking "Are you feeling OK today?" and then if the user answers "Yes" .... one response is written and any other answer produces the second response.

    What I need to know is - what if I want to write more than one response? For instance - what if I want there to be 4 different answers to the same question - with 4 different responses?

    It makes sense to me to write ....

    Code:
    var userAnswer = prompt("Are you feeling OK today?");
    if (userAnswer === "Yes")
    {
    	document.write("Great - glad to hear it");
    }
    
    if (userAnswer === "Not bad")
    {
    	document.write("Sorry to hear you are not feeling your best");
    }
    
    if (userAnswer === "so so")
    {
    	document.write("Great - glad to hear it");
    }
    
    else
    {
    	document.write("Oh dear! That is a shame!")
    }
    ....... but this obviously does not work! I have hunted around but either am not searching for the right thing or am missing the point!

    Can anyone please steer me in the right direction?

    The project is obviously much harder than this but I need to get the basics down! Eventually each question will have a set of 4 radio buttons and then the answers to the questions delivered on a page ......

    Ta in advance!

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,695
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    A prompt is always just a yes/no option. For multiple choice questions you need a form with radio buttons and validate the input (choice) upon submitting the form.

  • Users who have thanked VIPStephan for this post:

    if_only (01-24-2013)

  • #3
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    2
    Thanked 1 Time in 1 Post
    Seeing your codes and what you are trying to do can be also done with else if statements. Here is a sample from a site that I often go to http://www.w3schools.com/js/js_if_else.asp

    I think another way you can do something similar is using switch statement, which can be little more elegant on making multiple if statement. Here is another link from the same site. http://www.w3schools.com/js/js_switch.asp

    I newbie to javascript to, but there are a lot of useful site that explain the fundamental very well. Good luck with your javascript and keep at it .

  • Users who have thanked mosquitobite for this post:

    if_only (01-25-2013)

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    prompt provides three possible responses - to type in a string containing anything at all, to cancel and return nothing, or to turn off JavaScript completely and not return at all. In some browsers the last option is replaced by one that allows you to simply return nothing from this and all future prompts. It is an obsolete command with no real purpose in JavaScript for browsers more recent than Netscape 4.

    document.write became obsolete when Netscape 4 died. All browsers since then have provided better ways to interact with the web page from JavaScript. The simplest one for beginners to use (even simpler than document.write) is innerHTML.

    The w3schools JavaScript section is a history course demonstrating how JavaScript used to be written for use with Netscape browsers. The two guys who created the site have not updated it to cover the very different version of JavaScript used by modern browsers. If you are interested in how JavaScript used to be written then that site is a good resource but if you want to learn to actually write JavaScript then avoid it as much of what it teaches are things you need to stop using to get scripts to work properly in modern browsers.
    Last edited by felgall; 01-24-2013 at 06:26 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    if_only (01-25-2013)

  • #5
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Unhappy

    Thank you for your reply mate. I totally understand what you mean .... doing it is another thing altogether. Very basically (I realise the code isn't all there - just trying to explain!) ... I want to write:

    Q1
    How you feeling today?

    <input type="radio" name="feeling" value="not bad"
    <input type="radio" name="feeling" value="so so"
    <input type="radio" name="feeling" value="ok"
    <input type="radio" name="feeling" value="great"

    [hidden div appears over the original]

    Q2
    How you looking today?

    <input type="radio" name="looking" value="ugly"
    <input type="radio" name="looking" value="fair"
    <input type="radio" name="looking" value="quite good"
    <input type="radio" name="looking" value="stunning!"

    [hidden div appears over the last]

    There will be 3 questions and then finally the final hidden div will deliver the results to a form which can be emailed to me.

    I've found a few solutions and have been trying to develop some code that a kind coder gave me but no luck!

    To make things worse - I actually need it to eventually be cleverer than this ...... I don't actually want to know what they have selected. Each selection will be given a code and when the 3 selections are put together I need it to recommend a specific answer. In this case Feeling "so so" and Looking "Ugly" may suggest something like "Go for a Spa day!" ........ Each combination will be unique.

    Is there anyway you (or anyone) can start me off?! I'm going crazy!

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    My only sensible suggestion is ... learn Javascript. Presumably you would not attempt to service or repair a machine or appliance without any experience or understanding of how it worked. You would think much of a law student who attempted to carry out a surgical operation.

    You will need to use CSS to position your divs.

    "... a form which can be emailed to me."
    What would be the point of that when your questions are so trivial (pointless even)? In any case you will need server side scripting to send the contents of a web form by email.

    Modern browsers no longer accept mailto: as a form action - they simply open the email program (if any) and ignore the form. And many users will (sensibly) not wish to reveal their email address.

    document.write() has been obsolete since Netscape 3 passed away 10+ years ago. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    prompt() is also obsolete. And what if your user enters "yes" instead of "Yes"? Radio buttons are better.
    Last edited by Philip M; 01-24-2013 at 07:19 PM.

    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.

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by if_only View Post
    Q1
    How you feeling today?

    <input type="radio" name="feeling" value="not bad"
    <input type="radio" name="feeling" value="so so"
    <input type="radio" name="feeling" value="ok"
    <input type="radio" name="feeling" value="great"

    [hidden div appears over the original]

    Q2
    How you looking today?

    <input type="radio" name="looking" value="ugly"
    <input type="radio" name="looking" value="fair"
    <input type="radio" name="looking" value="quite good"
    <input type="radio" name="looking" value="stunning!"

    [hidden div appears over the last]

    There will be 3 questions and then finally the final hidden div will deliver the results to a form which can be emailed to me.
    At least with that you have a starting point for your code. With what you had in your original post there wasn't anything that was relevant to what you are trying to do.

    If you place all those radio button groups into a form that has a form2mail script attached to the action attribute then at lest you can get their selections mailed to where ever you want. Once you get that much working then you can look at what else you need to add to get it doing the rest of what you want.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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