Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Javascript if and else not working

Reply
Thread Tools

Javascript if and else not working

 
 
SA SA
Guest
Posts: n/a
 
      12-19-2006
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>

 
Reply With Quote
 
 
 
 
Janwillem Borleffs
Guest
Posts: n/a
 
      12-19-2006
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



 
Reply With Quote
 
 
 
 
Janwillem Borleffs
Guest
Posts: n/a
 
      12-19-2006
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


 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      12-19-2006
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".


--

 
Reply With Quote
 
SA SA
Guest
Posts: n/a
 
      12-20-2006
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


 
Reply With Quote
 
SA SA
Guest
Posts: n/a
 
      12-20-2006
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".
>
>
> --


 
Reply With Quote
 
SA SA
Guest
Posts: n/a
 
      12-20-2006
<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

 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      12-20-2006
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/
 
Reply With Quote
 
Matt Kruse
Guest
Posts: n/a
 
      12-20-2006
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


 
Reply With Quote
 
SA SA
Guest
Posts: n/a
 
      12-20-2006
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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
else if vs else { if A C++ 8 08-28-2010 08:55 PM
What's the use of the else in try/except/else? kj Python 15 05-23-2009 02:18 AM
for: else: - any practical uses for the else clause? metaperl.etc@gmail.com Python 25 09-30-2006 11:01 PM
ruby idiom for python's for/else while/else Gergely Kontra Ruby 16 09-20-2005 08:35 PM
AS5800 not working with Windows 95 - but works with everything else? Vinny Abello Cisco 14 12-10-2003 05:47 PM



Advertisments