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

B

bravesplace

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

Noah Sussman

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/code_examples/choose_value_with_2_select_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>
 
B

bravesplace

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.

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

VK

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

bravesplace

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

Randy Webb

(e-mail address removed) 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.
 
B

bravesplace

Thanks for everything. I appreciate the time.

(e-mail address removed) said the following on 11/8/2006 9:24 AM:

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/
 

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
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top