Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If then Else? 3

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
Is there a way to eliminate items appearing on the sendmail email if the values = 0. Is there an if then else structure in any way possible. Its to avoid a lengthy email and help format it a bit better. Thanks


document.ProxyForm.MessageBody.value = "\n\n" + "Order Placing" + "\n\n"
+ "Start of Order" + "\n\n"
+ "Time received: " + CurrentDate + "\n"
+ "DBC-6 " + DB6 + " off" + "\n"
+ "DBC-12 " + DB12 + " off" + "\n"
+ "DBC-22 " + DB22 + " off" + "\n"
+ "DBC-32 " + DB32 + " off" + "\n"
+ "DBC-34L " + DB34 + " off" + "\n"
+ "DBC-40 " + DB40 + " off" + "\n"
+ "DBC-64L " + DB64 + " off" + "\n"
+ "DBC-94L " + DB94 + " off" + "\n"
+ "DBC-124L " + DB124 + " off" + "\n\n" + "End of Oder, Thankyou";
 
Try this:
Code:
	_message = "\n\n" + "Order Placing" + "\n\n"
	+ "Start of Order" + "\n\n"
	+ "Time received: " + CurrentDate + "\n";
	if (parseInt(DB6,10)>0) _message += "DBC-6    " + DB6 + " off" + "\n"
	if (parseInt(DB12,10)>0) _message += "DBC-12   " + DB12 + " off" + "\n"
	if (parseInt(DB22,10)>0) _message += "DBC-22   " + DB22 + " off" + "\n"
	if (parseInt(DB32,10)>0) _message += "DBC-32   " + DB32 + " off" + "\n"
	if (parseInt(DB34,10)>0) _message += "DBC-34L   " + DB34 + " off" + "\n"
	if (parseInt(DB40,10)>0) _message += "DBC-40  " + DB40 + " off" + "\n"
	if (parseInt(DB64,10)>0) _message += "DBC-64L  " + DB64 + " off" + "\n" 
	if (parseInt(DB94,10)>0) _message += "DBC-94L  " + DB94 + " off" + "\n" 
	if (parseInt(DB124,10)>0) _message += "DBC-124L " + DB124 + " off" + "\n";
	_message += "\n" + "End of Order, Thankyou";
	document.ProxyForm.MessageBody.value = _message;
It's a bit clunky... but it'll do what you want I think!

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
If you add this function:

Code:
function ignoreBlank(prefix, value, suffix) {
	if (value != 0) return(prefix + value + suffix);
	return('');
}

and then use that to decide whether to output the lines or not, by adding ignoreBlank calls around the relevant lines:

Code:
document.ProxyForm.MessageBody.value = "\n\n" + "Order Placing" + "\n\n"
	+ "Start of Order" + "\n\n"
	+ "Time received: " + CurrentDate + "\n"
	+ ignoreBlank("DBC-6    ", DB6 ," off" + "\n")
	+ ignoreBlank("DBC-12   ", DB12 ," off" + "\n")
	+ ignoreBlank("DBC-22   ", DB22 ," off" + "\n")
	+ ignoreBlank("DBC-32   ", DB32 ," off" + "\n")
	+ ignoreBlank("DBC-34L   ", DB34 ," off" + "\n")
	+ ignoreBlank("DBC-40  ", DB40 ," off" + "\n")
	+ ignoreBlank("DBC-64L  ", DB64 ," off" + "\n")
	+ ignoreBlank("DBC-94L  ", DB94 ," off" + "\n")
	+ ignoreBlank("DBC-124L ", DB124 ," off" + "\n")
	+ "\n" + "End of Oder, Thankyou";

That should do much something similar to what you've asked.

You can change the criteria in the function quite easily to use othehr values other than 0, too.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks Jeff, Dan. Both did the trick. Both well deserved stars. Thanks also Dan, yes I don't think they would be very amused to thank them for their Oder!!

Just 1 query left, is there a way to pass the persons email address into the email body of the person sending the email. I know it gets put onto the head? of the email, but it would be good to come out with the order data. Many thanks again
 
Unfortunately it's the email client that puts their email address into the head of the email. So to answer your question... nope.

[smile]

Jeff


[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Are you using a backend on the server to process this information? With ASP, PHP, CFM or other on server processing you could have much greater control of the email.

mailto:emailaddres@domain.com?subject=Subject text&body=emailaddress@domain.com

The above will give the email client information for subject and body in an <a href tag.

xtendscott
Web Site Design | Cryosurgery | Walla Walla Portal
 
The hosted domain I have is not very helpful. Anything asked technically gets a response of "we can do your design for you"
I know I cannot put emails via the server, I have to use my Internet ISP. I should have spent longer looking around. Presumeably, mailto only works if email directed to the server? Correct or not. I am resorting to asking the user to enter his email into an input box, I can then use that in the email body. One day I'll grow big. Thanks aagain
 
the mailto works for straight HTML code.

<a href="mailto:emailaddres@domain.com?subject=Subject text&body=emailaddress@domain.com">Text here</a>

But I don't know enough about your script to know how it would work with that. The above mailto: creates a link that when clicked opens your email client with subject and Body information supplied after the "=".

Hope this helps.



xtendscott
Web Site Design | Cryosurgery | Walla Walla Portal
 
When I looked, I am using mailto, cracking up.
I am sending the contents of a form in an email.

This bit has the body message -
document.ProxyForm.MessageBody.value = _message;

This bit is the send button -

<form name="ProxyForm" enctype="text/plain" method="post" action ="mailto:fred@virgin.net?subject=Testing Only" onsubmit="return chkForm();">
<p class="CenteredContent"><input type="submit" value="Send Order"></input>
&lt; <b>Click To Place Order by email. Check your sent emails for
copy.</b></p>
<input type="hidden" id="MessageBody" name="ITV Order "></input>
</form>

If I could add the persons email address in the message it would be magic, otherwise I will have to ask them to put it in a form field. Many thanks
 
There is an even easier way to handle the original problem you asked about. Use the "conditional operator":
Code:
document.ProxyForm.MessageBody.value = "\n\n" + "Order Placing" + "\n\n"
  + "Start of Order" + "\n\n"
  + "Time received: " + CurrentDate + "\n"
  + (DB6   > 0 ? "DBC-6    " + DB6   + " off" + "\n" : "")
  + (DB12  > 0 ? "DBC-12   " + DB12  + " off" + "\n" : "")
  + (DB22  > 0 ? "DBC-22   " + DB22  + " off" + "\n" : "")
  + (DB32  > 0 ? "DBC-32   " + DB32  + " off" + "\n" : "")
  + (DB34  > 0 ? "DBC-34L  " + DB34  + " off" + "\n" : "")
  + (DB40  > 0 ? "DBC-40   " + DB40  + " off" + "\n" : "")
  + (DB64  > 0 ? "DBC-64L  " + DB64  + " off" + "\n" : "")
  + (DB94  > 0 ? "DBC-94L  " + DB94  + " off" + "\n" : "")
  + (DB124 > 0 ? "DBC-124L " + DB124 + " off" + "\n" : "")
  + "\nEnd of Order, Thankyou";
(Note that I had to change the \n's on last two lines to maintain the extra \n before the end message even if DB124 is not > 0.)
The format of the conditional operator is:
Code:
  condition ? true-value : false-value
If the condition is true, the true-value is used, if not, the false-value is used.

It's extremely useful for embedding "use this string if this condition or that string if not this condition" type logic in other statements.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thank you very much Tracy.

Can you tell me how I could break the code up further with an ELSE part. Basically there will be more items in the selected list, and I need somehow to break parts up for titles, in other words making the email easier to understand.
I have bodged the code below to show what I mean, and would
be a very happy person to solve it.

Many thanks again


document.ProxyForm.MessageBody.value = "\n\n" + "Order Placing" + "\n\n"
+ "Start of Order" + "\n\n"
+ "Time received: " + CurrentDate + "\n"

IF ANY DB6-DB124 ITEMS SELECTED (ie)>0 then
PUT IN A TITLE HERE, followed by a space.

+ (DB6 > 0 ? "DBC-6 " + DB6 + " off" + "\n" : "")
+ (DB12 > 0 ? "DBC-12 " + DB12 + " off" + "\n" : "")
+ (DB22 > 0 ? "DBC-22 " + DB22 + " off" + "\n" : "")
+ (DB32 > 0 ? "DBC-32 " + DB32 + " off" + "\n" : "")
+ (DB34 > 0 ? "DBC-34L " + DB34 + " off" + "\n" : "")
+ (DB40 > 0 ? "DBC-40 " + DB40 + " off" + "\n" : "")
+ (DB64 > 0 ? "DBC-64L " + DB64 + " off" + "\n" : "")
+ (DB94 > 0 ? "DBC-94L " + DB94 + " off" + "\n" : "")
+ (DB124 > 0 ? "DBC-124L " + DB124 + " off" + "\n" : "")

ELSE
COMES HERE IF NO ITEMS ABOVE SELECTED

IF ANY SB6-SB124 ITEMS SELECTED (ie)>0 then
PUT IN A TITLE HERE, followed by a space.

+ (SB6 > 0 ? "SBC-6 " + SB6 + " off" + "\n" : "")
+ (SB12 > 0 ? "SBC-12 " + SB12 + " off" + "\n" : "")
+ (SB22 > 0 ? "SBC-22 " + SB22 + " off" + "\n" : "")
+ (SB32 > 0 ? "SBC-32 " + SB32 + " off" + "\n" : "")
+ (SB34 > 0 ? "SBC-34L " + SB34 + " off" + "\n" : "")
+ (SB40 > 0 ? "SBC-40 " + SB40 + " off" + "\n" : "")
+ (SB64 > 0 ? "SBC-64L " + SB64 + " off" + "\n" : "")
+ (SB94 > 0 ? "SBC-94L " + SB94 + " off" + "\n" : "")
+ (SB124 > 0 ? "SBC-124L " + SB124 + " off" + "\n" : "")




+ "\nEnd of Order, Thankyou";
 
I think the way I would handle the "if any" part is to create a string from the DBx options. If the string ends up null, then no options were selected. If not null, then at least one was. Then you can concatenate that string into the email text.
Code:
var DBstr = ""
  + (DB6   > 0 ? "DBC-6    " + DB6   + " off" + "\n" : "")
  + (DB12  > 0 ? "DBC-12   " + DB12  + " off" + "\n" : "")
  + (DB22  > 0 ? "DBC-22   " + DB22  + " off" + "\n" : "")
  + (DB32  > 0 ? "DBC-32   " + DB32  + " off" + "\n" : "")
  + (DB34  > 0 ? "DBC-34L  " + DB34  + " off" + "\n" : "")
  + (DB40  > 0 ? "DBC-40   " + DB40  + " off" + "\n" : "")
  + (DB64  > 0 ? "DBC-64L  " + DB64  + " off" + "\n" : "")
  + (DB94  > 0 ? "DBC-94L  " + DB94  + " off" + "\n" : "")
  + (DB124 > 0 ? "DBC-124L " + DB124 + " off" + "\n" : "");

var SBstr = ""
  + (SB6   > 0 ? "SBC-6    " + SB6   + " off" + "\n" : "")
  + (SB12  > 0 ? "SBC-12   " + SB12  + " off" + "\n" : "")
  + (SB22  > 0 ? "SBC-22   " + SB22  + " off" + "\n" : "")
  + (SB32  > 0 ? "SBC-32   " + SB32  + " off" + "\n" : "")
  + (SB34  > 0 ? "SBC-34L  " + SB34  + " off" + "\n" : "")
  + (SB40  > 0 ? "SBC-40   " + SB40  + " off" + "\n" : "")
  + (SB64  > 0 ? "SBC-64L  " + SB64  + " off" + "\n" : "")
  + (SB94  > 0 ? "SBC-94L  " + SB94  + " off" + "\n" : "")
  + (SB124 > 0 ? "SBC-124L " + SB124 + " off" + "\n" : "");

document.ProxyForm.MessageBody.value = "\n\n" + "Order Placing" + "\n\n"
  + "Start of Order" + "\n\n"
  + "Time received: " + CurrentDate + "\n"
  + (DBstr != "" ? "At least one DB item ordered\n" + DBstr + "\n\n" : "")
  + (SBstr != "" ? "At least one SB item ordered\n" + SBstr + "\n\n" : "")
  + "End of Order, Thankyou";
Does that look like what you want?


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
If the test for each field will always be the same, then I'd stick with my function method - as changing the logic can be done by changing one line only.

That's not intended in any way as a criticism of any other method shown, for they're all good... just a general observation.

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks very much Tracy, absolutely perfect. Have a star, a good weekend, and thanks again.
Thankyou as well Dan, you to have a good weekend.
 
Thanks for the star. Glad I could help. Hope your weekend is good too.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top