Javascript if and else not working

S

SA SA

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

Janwillem Borleffs

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

Use:

document.login.PMT_INDICATOR.value

Or:

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


JW
 
J

Janwillem Borleffs

Janwillem said:
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
 
L

Lee

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".


--
 
S

SA SA

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 said:
Janwillem said:
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
 
S

SA SA

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

SA SA

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

Randy Webb

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

Matt Kruse

SA said:
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
 
S

SA SA

L

Lee

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.


--
 
D

Dr J R Stockton

In comp.lang.javascript message
Tue 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 (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.
 
S

SA SA

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top