www.webdeveloper.com
Results 1 to 5 of 5

Thread: using If else if else statement getting error

  1. #1
    Join Date
    Nov 2013
    Posts
    2

    using If else if else statement getting error

    I am newbie to javascript I trying to code conditions for currency change based on shipping country but getting error for all conditions.

    Conditions

    if the country is India and currency is Indian Rupees - Proceed to next page
    if the country is India and currency is not India Rupees - alert as "Change currency as Indian Rupees"
    if the country is not India and currency is India Rupees - alert as "Change currency as other than Indian Rupees"
    if the country is not India and currency is not Indian Rupees - Proceed to next page

    Help me to correct the error.

    Thanks in advance.

    Code:
    <div id="navMainSearch" style="height: 50px; margin-top: 18px; margin-bottom: -15px;">
            <form method="get" action="" name="currencies">
                <select id="money" onchange="" name="currency">
                    <option value="USD">US Dollar</option>
                    <option value="EUR">Euro</option>
                    <option value="GBP">British Pound Sterling</option>
                    <option value="CAD">Canadian Dollar</option>
                    <option value="AUD">Australian Dollar</option>
                    <option value="INR">Indian Rupees</option>
                </select>
                <input type="hidden" value="checkout" name="main_page">
                <input type="hidden" name="fecaction">
            </form>
        </div>
        <div id="checkoutShipto" class="floatingBox back">
    <address class="checkoutAddress">
    Raja 
    <br>
    423, Vinayagar Kovil Street
    <br>
    Chennai, 600027
    <br>
    TN,India<div id="location">India</div>
    </address>
    
        </div>
        <div class="buttonRow forward">
            <input type="submit" onclick="submitFunction()" title="Confirm Order " alt="Confirm Order" value="submit" act>
        </div>
        </div>
        <script type="text/javascript">
    function submitFunction() {
        var country = document.getElementById("location").innerText;
        var currency = document.getElementById("money").innerText;
        if (country == 'India' && currency == 'INR') {
            window.location.assign("thanks.html");
        } else if (country == 'India' && currency != 'INR') {
            alert("Change the currency as Indian Rupee ");
        } else if (country != 'India' && currency == 'INR') {
            alert("Change the currency as other than Indian Rupee ");
        } else {
            window.location.assign("thanks.html ");
        }
    
    }
    </script>

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    Code:
    function submitFunction(){
    var country=document.getElementById("location").innerHTML,
    sel=document.getElementById("money"),
    currency=sel[sel.selectedIndex].value;
    if(country == 'India' && currency == 'INR'){window.location.assign("thanks.html");}
    else if(country == 'India' && currency != 'INR'){alert("Change the currency as Indian Rupee ");}
    else if(country != 'India' && currency == 'INR'){alert("Change the currency as other than Indian Rupee ");}
    }

  3. #3
    Join Date
    Nov 2013
    Posts
    2
    Thanks a lot.
    Your Great Thanks again.......

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    there exist other (more flexible) ways of that check. if you have several countries and several currencies you can, for example, create the array which every member will look like:

    ['country',['currency_1','currency_2',etc.'currency_n']]

    and then you'll run two loops. the first loop finds 'country' looking over the first members of each member. if it doesn't match the loop skips the member (continue), but if it matches the first member the second loop which looks over the second member (the array of acceptable currencies) starts. if the second loop finds the given currency it breaks and submits the form.

    anyway, you must understand that the better check is done at the server-side

  5. #5
    Join Date
    Mar 2012
    Posts
    1,814
    Quote Originally Posted by Padonak View Post
    Code:
    function submitFunction(){
    var country=document.getElementById("location").innerHTML,
    sel=document.getElementById("money"),
    currency=sel[sel.selectedIndex].value;
    if(country == 'India' && currency == 'INR'){window.location.assign("thanks.html");}
    else if(country == 'India' && currency != 'INR'){alert("Change the currency as Indian Rupee ");}
    else if(country != 'India' && currency == 'INR'){alert("Change the currency as other than Indian Rupee ");}
    }
    Just a small point. The above code works, but the test for "country != 'India'" is redundant. This is because, if the country IS 'India', one or other of the previous two tests MUST be true. So it can be simplified to:

    else if(currency == 'INR'){alert("Change the currency as other than Indian Rupee ");}

    Also, I'd echo the view that this test should be done server-side, as it is too easy for client-side validation to be bypassed. So you will NEED to validate server-side anyway. Validating both client and server-side is not a good idea because, if/when the criteria change, its very easy to forget that you need to amend two places! So you can get obscure errors because the client and server side validations differ.
    Last edited by jedaisoul; 11-18-2013 at 10:16 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles