Javascript if and else not working

Discussion in 'Javascript' started by SA SA, Dec 19, 2006.

  1. SA SA

    SA SA Guest

    I know it is me but i can not get this condition to work. Regardless of
    Check or credit card it always defaults to else portion of the script.
    I can't get if (login.PMT_INDICATOR.value == "C") to be true. What i am
    doing wrong? please help







    <html>

    <form method="post" name="login" id="login" >
    <input type="hidden" Value ="Account Balance"
    name="PMT_TYPE_DESC" >
    <input type="radio" Value ="" name="PMT_INDICATOR"> Check
    (Checking/Saving Account)<p>
    ] <input type="radio" Value ="C" checked name="PMT_INDICATOR" >
    Credit Cards</p><p>
    <input type=button name=submit1 value="Login"
    Onclick="SubmitForm(login);">
    </p>
    </form>

    <script type="text/javascript">
    <!--

    function SubmitForm(login)
    {

    if (login.PMT_INDICATOR.value == "C")
    {
    login.action = "https://creditcard.gateway.com/Gateway.aspx";
    }
    else
    {
    login.action = "https://Check.gateway.com/Gateway.aspx";
    }
    login.submit(); // Submit the page
    return true;
    }

    //-->
    </script>



    </html>
     
    SA SA, Dec 19, 2006
    #1
    1. Advertising

  2. SA SA wrote:
    > I know it is me but i can not get this condition to work. Regardless
    > of Check or credit card it always defaults to else portion of the
    > script. I can't get if (login.PMT_INDICATOR.value == "C") to be true.
    > What i am doing wrong? please help
    >


    Use:

    document.login.PMT_INDICATOR.value

    Or:

    document.forms['login'].elements['PMT_INDICATOR'].value


    JW
     
    Janwillem Borleffs, Dec 19, 2006
    #2
    1. Advertising

  3. Janwillem Borleffs wrote:
    > document.forms['login'].elements['PMT_INDICATOR'].value
    >


    OTOH, you could fix it by changing the call to submitForm into:

    <input type=button name=submit1 value="Login"
    Onclick="SubmitForm(this.form);">


    JW
     
    Janwillem Borleffs, Dec 19, 2006
    #3
  4. SA SA

    Lee Guest

    SA SA said:
    >
    >I know it is me but i can not get this condition to work. Regardless of
    >Check or credit card it always defaults to else portion of the script.
    >I can't get if (login.PMT_INDICATOR.value == "C") to be true. What i am
    >doing wrong? please help


    If an expression that you think should be true, isn't, add an alert()
    to show you what the value really is.
    In this case:

    alert(login.PMT_INDICATOR.value)

    will show you that the value is never "C".


    --
     
    Lee, Dec 19, 2006
    #4
  5. SA SA

    SA SA Guest

    JW,
    I am using form id instead of this.form which i think i almost the
    same. I tried that before. It is not that. Any other idea.
    sa

    Janwillem Borleffs wrote:
    > Janwillem Borleffs wrote:
    > > document.forms['login'].elements['PMT_INDICATOR'].value
    > >

    >
    > OTOH, you could fix it by changing the call to submitForm into:
    >
    > <input type=button name=submit1 value="Login"
    > Onclick="SubmitForm(this.form);">
    >
    >
    > JW
     
    SA SA, Dec 20, 2006
    #5
  6. SA SA

    SA SA Guest

    Lee,
    When i did that it tells me PMT_INDICATOR is undefined. It tried this
    but did not work either.



    <form id="login" name="login" method="post"><input type="hidden"
    value="Account Balance" name="PMT_TYPE_DESC"><input type="radio"
    value="" name="PMT_INDICATOR"> Check (Checking/Saving Account)

    <p>] <input type="radio" checked value="C" name="PMT_INDICATOR"> Credit
    Cards</p>

    <p><input onclick="SubmitForm(this.form, login.PMT_INDICATOR.value);"
    type="button" value="Login" name="submit1"></p>
    </form>
    <script type="text/javascript">
    <!--

    function SubmitForm(login, PMT_INDICATOR)
    {
    var pmt = PMT_INDICATOR
    if (pmt == "C")
    {
    alert(pmt)
    login.action = "https://creditcard.gateway.com/Gateway.aspx";
    }
    else
    {
    alert(pmt)
    login.action = "https://Check.gateway.com/Gateway.aspx";
    }
    login.submit(); // Submit the page
    return true;
    }

    //-->
    </script>




    Lee wrote:
    > SA SA said:
    > >
    > >I know it is me but i can not get this condition to work. Regardless of
    > >Check or credit card it always defaults to else portion of the script.
    > >I can't get if (login.PMT_INDICATOR.value == "C") to be true. What i am
    > >doing wrong? please help

    >
    > If an expression that you think should be true, isn't, add an alert()
    > to show you what the value really is.
    > In this case:
    >
    > alert(login.PMT_INDICATOR.value)
    >
    > will show you that the value is never "C".
    >
    >
    > --
     
    SA SA, Dec 20, 2006
    #6
  7. SA SA

    SA SA Guest

    <input type="radio" value="" name="PMT_INDICATOR"> Check
    (Checking/Saving Account)
    <input type="radio" checked value="C" name="PMT_INDICATOR"> Credit
    Cards

    After taking this a part, it turns out the problem is with two check
    boxes with same name. is there any way to get around this problem?

    thanks
     
    SA SA, Dec 20, 2006
    #7
  8. SA SA

    Randy Webb Guest

    SA SA said the following on 12/19/2006 10:37 PM:
    > <input type="radio" value="" name="PMT_INDICATOR"> Check
    > (Checking/Saving Account)
    > <input type="radio" checked value="C" name="PMT_INDICATOR"> Credit
    > Cards
    >
    > After taking this a part, it turns out the problem is with two check
    > boxes with same name.


    I don't even see one checkbox, much less two.

    > is there any way to get around this problem?


    What problem?

    <URL: http://jibbering.com/faq/index.html#FAQ4_13>
    <URL: http://www.jibbering.com/faq/faq_notes/form_access.html#faBut>

    --
    Randy
    Chance Favors The Prepared Mind
    comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    Randy Webb, Dec 20, 2006
    #8
  9. SA SA

    Matt Kruse Guest

    SA SA wrote:
    > After taking this a part, it turns out the problem is with two check
    > boxes with same name. is there any way to get around this problem?


    Yes, but you have many more problems than just that.
    Try this, which includes several fixes [untested]:

    <form id="login" name="login" method="post"
    action="https://Check.gateway.com/Gateway.aspx"
    onsubmit="return checkform(this)">
    <input type="hidden" value="Account Balance" name="PMT_TYPE_DESC">
    <input type="radio" value="" name="PMT_INDICATOR"> Check (Checking/Saving
    Account)
    <input type="radio" checked value="C" name="PMT_INDICATOR"> Credit Cards
    <input type="submit" onclick="SubmitForm(this.form,
    login.PMT_INDICATOR.value);" type="button" value="Login" name="submit1">
    </form>

    <script type="text/javascript">
    function checkform(f) {
    if (f.PMT_INDICATOR[0].checked) {
    f.action = "https://creditcard.gateway.com/Gateway.aspx";
    }
    return true;
    }
    </script>

    But, as a general rule, changing the action attribute of a form points to
    bad design. If script is disabled, for example, your form breaks. Business
    logic should be done on the server side.

    As a final suggestion, see
    http://www.javascripttoolbox.com/bestpractices/#forms

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
     
    Matt Kruse, Dec 20, 2006
    #9
  10. SA SA

    SA SA Guest

    meant to say radio buttons.
    sa


    Randy Webb wrote:
    > SA SA said the following on 12/19/2006 10:37 PM:
    > > <input type="radio" value="" name="PMT_INDICATOR"> Check
    > > (Checking/Saving Account)
    > > <input type="radio" checked value="C" name="PMT_INDICATOR"> Credit
    > > Cards
    > >
    > > After taking this a part, it turns out the problem is with two check
    > > boxes with same name.

    >
    > I don't even see one checkbox, much less two.
    >
    > > is there any way to get around this problem?

    >
    > What problem?
    >
    > <URL: http://jibbering.com/faq/index.html#FAQ4_13>
    > <URL: http://www.jibbering.com/faq/faq_notes/form_access.html#faBut>
    >
    > --
    > Randy
    > Chance Favors The Prepared Mind
    > comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
    > Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
     
    SA SA, Dec 20, 2006
    #10
  11. SA SA

    Lee Guest

    SA SA said:
    >
    ><input type="radio" value="" name="PMT_INDICATOR"> Check
    >(Checking/Saving Account)
    ><input type="radio" checked value="C" name="PMT_INDICATOR"> Credit
    >Cards
    >
    >After taking this a part, it turns out the problem is with two check
    >boxes with same name. is there any way to get around this problem?


    They're radio boxes, and the Radio object doesn't have a "value" attribute.
    See the FAQ regarding how to access the values of form elements.


    --
     
    Lee, Dec 20, 2006
    #11
  12. In comp.lang.javascript message
    <>, Tue, 19 Dec 2006
    14:17:11, SA SA <> wrote:
    >I know it is me but i can not get this condition to work. Regardless of
    >Check or credit card it always defaults to else portion of the script.
    >I can't get if (login.PMT_INDICATOR.value == "C") to be true. What i am
    >doing wrong? please help



    >if (login.PMT_INDICATOR.value == "C")
    >{
    >login.action = "https://creditcard.gateway.com/Gateway.aspx";
    >}
    >else
    >{
    >login.action = "https://Check.gateway.com/Gateway.aspx";
    >}


    Clearly you have made a mistake.

    Since you think the conditional statement is not working, examine the
    condition. Probably "C" is safe, so put before that line

    alert(login.PMT_INDICATOR.value)

    to see what value the left side actually has. You will, I think, see
    that its value is always "undefined".

    So your REAL problem is that you have no notion of elementary debugging.

    <URL:http://www.merlyn.demon.co.uk/js-other.htm#Dbg>

    It's a good idea to read the newsgroup and its FAQ. See below.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    Dr J R Stockton, Dec 20, 2006
    #12
  13. SA SA

    SA SA Guest

    I am full time DBA, never touch this before. I was given two gateways
    by our payment processing company. I was asked to post hidden
    attributes to a gateway based upon credit or check payment.

    All i need is:

    1. Check
    2. Credit

    if 1 then
    post to = "credit gateway"
    else
    post to = "Check Gateway"



    I got Matt's suggestion to work with my need. I will trash my idea and
    run with his.

    sa







    Dr J R Stockton wrote:
    > In comp.lang.javascript message
    > <>, Tue, 19 Dec 2006
    > 14:17:11, SA SA <> wrote:
    > >I know it is me but i can not get this condition to work. Regardless of
    > >Check or credit card it always defaults to else portion of the script.
    > >I can't get if (login.PMT_INDICATOR.value == "C") to be true. What i am
    > >doing wrong? please help

    >
    >
    > >if (login.PMT_INDICATOR.value == "C")
    > >{
    > >login.action = "https://creditcard.gateway.com/Gateway.aspx";
    > >}
    > >else
    > >{
    > >login.action = "https://Check.gateway.com/Gateway.aspx";
    > >}

    >
    > Clearly you have made a mistake.
    >
    > Since you think the conditional statement is not working, examine the
    > condition. Probably "C" is safe, so put before that line
    >
    > alert(login.PMT_INDICATOR.value)
    >
    > to see what value the left side actually has. You will, I think, see
    > that its value is always "undefined".
    >
    > So your REAL problem is that you have no notion of elementary debugging.
    >
    > <URL:http://www.merlyn.demon.co.uk/js-other.htm#Dbg>
    >
    > It's a good idea to read the newsgroup and its FAQ. See below.
    >
    > --
    > (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
    > news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
    > <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
    > <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
     
    SA SA, Dec 20, 2006
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    25
    Views:
    702
    MonkeeSage
    Oct 1, 2006
  2. #else not working

    , Jul 6, 2006, in forum: C++
    Replies:
    6
    Views:
    347
  3. kj
    Replies:
    15
    Views:
    605
    Lawrence D'Oliveiro
    May 23, 2009
  4. A
    Replies:
    8
    Views:
    841
    James Kanze
    Aug 28, 2010
  5. Gergely Kontra
    Replies:
    16
    Views:
    254
    Devin Mullins
    Sep 20, 2005
Loading...

Share This Page