By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,241 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

A "How would you go about coding this" question.

P: n/a
I am hoping for a code example of how to do this, and hopefully it will

help me to see an easy way to code what seems to be a huge monster.

I need to create a form that has two pulldown menues (field names
Option1 and Option2). Each will house a list of the 50 U.S. States.
Users would then be able to choose two states, and get a listing for a
courier used for pickup\deliveries (exp: My order is in Ohio, but I
need it delivered to Maine. Who do I use?)
I am not as familiar with code as I would like, but it seems I would
need something like this:
If option1 = this, and option2 = this, then the answer (field name
Result) = this
I know that looks stupid, but I am hoping to show you how I am
approching this in my head.
Any ideas on how you would approch this project would be great. If you
could offer a sample of code, I'm sure I could use it as a template for

other projects. My idea is that this is just standard IF this THEN that

coding, but I don't know the basics enough to create the code from
scratch.
Thanks for any time you can give.

Nov 6 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
br*********@yahoo.com wrote:
I am hoping for a code example of how to do this, and hopefully it will

help me to see an easy way to code what seems to be a huge monster.

I need to create a form that has two pulldown menues (field names
Option1 and Option2). Each will house a list of the 50 U.S. States.
Users would then be able to choose two states, and get a listing for a
courier used for pickup\deliveries (exp: My order is in Ohio, but I
need it delivered to Maine. Who do I use?)
You can build a lookup table object, using JavaScript object notation.
If you are familiar with hashes, you can also think of this table as a
2-dimensional hash.

var sendFromTo = {
Alaska : {
Alaska : "local mail",
Alabama : "FedEx"
},
Alabama: {
Alaska : "UPS",
Alabama : "pony express"
}
}

Here is an example lookup against this table:

sendFromTo["Alaska"]["Alabama"] //evaluates to "Fedex"
>
I am not as familiar with code as I would like, but it seems I would
need something like this:
If option1 = this, and option2 = this, then the answer (field name
Result) = this
If you use this technique, you don't need a conditional. Instead,
after the user has chosen one value from each menu, submit those two
values to the table, and then display the result.

var from = firstMenu[firstMenu.selectedIndex].text;
var destination = secondMenu[secondMenu.selectedIndex].text;

var msg = sendFromTo[from][destination];
//now display the value of "msg" to the user

Here is the code of a brief, working example of this kind of solution.
You can see it in action at
http://onemorebug.com/meme.washer/co...ect_menus.html
Hope this helps.

<script type="text/javascript">
var sendFromTo = {
Alaska : {
Alaska : "local mail",
Alabama : "FedEx"
},
Alabama: {
Alaska : "UPS",
Alabama : "pony express"
}
}

function getShippingMethod () {
var firstMenu = document.forms[0].elements[0];
var secondMenu = document.forms[0].elements[1];
if (firstMenu.selectedIndex != 0 && secondMenu.selectedIndex !=0) {
// console.warn(1);
var from = firstMenu[firstMenu.selectedIndex].text;
var destination = secondMenu[secondMenu.selectedIndex].text;
var msg = sendFromTo[from][destination];
//alert(msg);
document.getElementById('youChose').innerHTML = msg;
}
}
</script>

<form>
Ship From:
<select onchange="getShippingMethod()">
<option>Select a state</option>
<option>Alaska</option>
<option>Alabama</option>
</select>
<br>
Send To:
<select onchange="getShippingMethod()">
<option>Select a state</option>
<option>Alaska</option>
<option>Alabama</option>
</select>
</form>

<div>Send by method: <span id="youChose">No method selected</span></div>

Nov 6 '06 #2

P: n/a
WOW! Thanks so much. I appreciate you offering this examples. It makes
it so much easier when you can SEE it.

Have a great day, and thanks again for all your help.

On Nov 6, 12:40 pm, "Noah Sussman" <blink...@gmail.comwrote:
bravespl...@yahoo.com wrote:
I am hoping for a code example of how to do this, and hopefully it will
help me to see an easy way to code what seems to be a huge monster.
I need to create a form that has two pulldown menues (field names
Option1 and Option2). Each will house a list of the 50 U.S. States.
Users would then be able to choose two states, and get a listing for a
courier used for pickup\deliveries (exp: My order is in Ohio, but I
need it delivered to Maine. Who do I use?)You can build a lookup table object, using JavaScript object notation.
If you are familiar with hashes, you can also think of this table as a
2-dimensional hash.

var sendFromTo = {
Alaska : {
Alaska : "local mail",
Alabama : "FedEx"
},
Alabama: {
Alaska : "UPS",
Alabama : "pony express"
}

}Here is an example lookup against this table:

sendFromTo["Alaska"]["Alabama"] //evaluates to "Fedex"
I am not as familiar with code as I would like, but it seems I would
need something like this:
If option1 = this, and option2 = this, then the answer (field name
Result) = thisIf you use this technique, you don't need a conditional. Instead,
after the user has chosen one value from each menu, submit those two
values to the table, and then display the result.

var from = firstMenu[firstMenu.selectedIndex].text;
var destination = secondMenu[secondMenu.selectedIndex].text;

var msg = sendFromTo[from][destination];
//now display the value of "msg" to the user

Here is the code of a brief, working example of this kind of solution.
You can see it in action athttp://onemorebug.com/meme.washer/code_examples/choose_value_with_2_s...
Hope this helps.

<script type="text/javascript">
var sendFromTo = {
Alaska : {
Alaska : "local mail",
Alabama : "FedEx"
},
Alabama: {
Alaska : "UPS",
Alabama : "pony express"
}

}function getShippingMethod () {
var firstMenu = document.forms[0].elements[0];
var secondMenu = document.forms[0].elements[1];
if (firstMenu.selectedIndex != 0 && secondMenu.selectedIndex !=0) {
// console.warn(1);
var from = firstMenu[firstMenu.selectedIndex].text;
var destination = secondMenu[secondMenu.selectedIndex].text;
var msg = sendFromTo[from][destination];
//alert(msg);
document.getElementById('youChose').innerHTML = msg;
}}</script>

<form>
Ship From:
<select onchange="getShippingMethod()">
<option>Select a state</option>
<option>Alaska</option>
<option>Alabama</option>
</select>
<br>
Send To:
<select onchange="getShippingMethod()">
<option>Select a state</option>
<option>Alaska</option>
<option>Alabama</option>
</select>
</form>

<div>Send by method: <span id="youChose">No method selected</span></div>
Nov 6 '06 #3

P: n/a
VK

br*********@yahoo.com wrote:
other projects. My idea is that this is just standard IF this THEN that
coding
It is if you always have one-to-one relations (say for a parsel from
California to New Jersy there is only one possible combo of carriers).
It is more complicated if you (may) have one-to-many relations (say for
the above mentioned parsel there are three possible combo different by
price and terms).

Please alaborate on that.

Nov 6 '06 #4

P: n/a
Its one-to-one, and your example will work perfectly. Could you give me
some idea of the complications with a one-to-many. I would assume I
will have to cross that bridge one day. Thanks again for the time.

On Nov 6, 12:58 pm, "VK" <schools_r...@yahoo.comwrote:
bravespl...@yahoo.com wrote:
other projects. My idea is that this is just standard IF this THEN that
codingIt is if you always have one-to-one relations (say for a parsel from
California to New Jersy there is only one possible combo of carriers).
It is more complicated if you (may) have one-to-many relations (say for
the above mentioned parsel there are three possible combo different by
price and terms).

Please alaborate on that.
Nov 8 '06 #5

P: n/a
br*********@yahoo.com said the following on 11/8/2006 9:24 AM:
Its one-to-one, and your example will work perfectly. Could you give me
some idea of the complications with a one-to-many. I would assume I
will have to cross that bridge one day. Thanks again for the time.
One to many is not that difficult, it's just easier not to use a simple
array to do it. An array of arrays where the main array holds the state,
the child array holds the multiple shippers.

Not sure what you are working on but another potential problem you are
going to run into is shipping across a state line. If you wanted to ship
a 1 pound box from Mobile Alabama to Savannah Georgia, you would use a
shipper, all is fine. But if you want to ship that same 1 pound box from
Phenix City Alabama to Columbus Georgia then the scenario changes (both
are shipped from AL to GA). The difference is Phenix City to Columbus is
about 4 feet and a state line. Mobile to Savannah isn't that simple.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 8 '06 #6

P: n/a
Thanks for everything. I appreciate the time.

On Nov 8, 11:21 am, Randy Webb <HikksNotAtH...@aol.comwrote:
bravespl...@yahoo.com said the following on 11/8/2006 9:24 AM:
Its one-to-one, and your example will work perfectly. Could you give me
some idea of the complications with a one-to-many. I would assume I
will have to cross that bridge one day. Thanks again for the time.One to many is not that difficult, it's just easier not to use a simple
array to do it. An array of arrays where the main array holds the state,
the child array holds the multiple shippers.

Not sure what you are working on but another potential problem you are
going to run into is shipping across a state line. If you wanted to ship
a 1 pound box from Mobile Alabama to Savannah Georgia, you would use a
shipper, all is fine. But if you want to ship that same 1 pound box from
Phenix City Alabama to Columbus Georgia then the scenario changes (both
are shipped from AL to GA). The difference is Phenix City to Columbus is
about 4 feet and a state line. Mobile to Savannah isn't that simple.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/
Nov 8 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.