Results 1 to 7 of 7

Thread: Forcing a value into a text box from a combo selection

  1. #1
    Join Date
    Apr 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Forcing a value into a text box from a combo selection

    Hi,

    I am trying to force a value in a text box from selecting a combo box but for some reason I cant get the IF statement working.
    I want to be able to select a value from the combo box and when doing so it will put a number value in the text box.


    <script Language="JavaScript">
    <!--

    function compute(form)
    {

    if (baskets=="toast")
    {
    document.basketorder.price.value = 40;
    }

    }

    //-->
    </script>
    <body>

    <FORM METHOD="POST" ACTION="http://mailgate.server-mail.com/cgi-bin/mailgate" name="basketorder">

    <select name="baskets" onClick="compute(this.form)">
    <option value="">- Select -</option>
    <option value="toast">A Toast</option>
    <option value="simple">Simple Pleasures</option>
    <option value="fine">Fine Fare</option>
    <option value="party">Party Time</option>
    </select>

    <input name="price" type="text">
    </form>

    Thanks for your help with this.

    Frogger

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <script type="text/javascript">
      var prices = new Object();
      prices.toast = 40;
      prices.simple = 50;
      prices.fine = 60.5;
      prices.party = 112.34;
    </script>
    
    <body>
      <form method="post" action="http://mailgate.server-mail.com/cgi-bin/mailgate" name="basketorder">
        <select name="baskets" onchange="this.form.elements['price'].value = (typeof prices[this.value] != 'undefined' ? prices[this.value] : '');">
          <option value="" disabled="true" selected="true">- Select -</option>
          <option value="toast">A Toast</option>
          <option value="simple">Simple Pleasures</option>
          <option value="fine">Fine Fare</option>
          <option value="party">Party Time</option>
        </select>
        <input name="price" type="text" readonly="true">
      </form>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Twey,

    Just wondering if I could ask for your help one more time.
    I have added 2 more fields but I want to be able to make the final total value change when either the - select - or -qty - combo boxes are changed.
    Meaning that total input box will show a new value anything time the 2 combo boxes change.

    Thanks
    Frogger

    <script type="text/javascript">
    var prices = new Object();
    prices.toast = 40;
    prices.simple = 50;
    prices.fine = 60.5;
    prices.party = 112.34;


    function compute(form)
    {

    total = (eval(form.price.value * form.qty.value));

    document.basketorder.total.value = total;


    }


    </script>



    <body>
    <form method="post" action="http://mailgate.server-mail.com/cgi-bin/mailgate" name="basketorder">

    <select name="baskets" onchange="this.form.elements['price'].value = (typeof prices[this.value] != 'undefined' ? prices[this.value] : '');">
    <option value="" disabled="true" selected="true">- Select -</option>
    <option value="toast">A Toast</option>
    <option value="simple">Simple Pleasures</option>
    <option value="fine">Fine Fare</option>
    <option value="party">Party Time</option>
    </select>

    <input name="price" type="text" readonly="true" onFocus="this.blur()">

    <select name="qty" onchange="compute(this.form)">
    <option value="" disabled="true" selected="true">- Qty -</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>

    </select>

    <input name="total" type="text" onFocus="this.blur()">

    <input type="submit" name="Submit" value="Submit">
    <input type="reset" name="reset" value="Reset">

    </form>

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    <select name="baskets" onchange="this.form.elements['price'].value = ((typeof prices[this.value] != 'undefined' ? prices[this.value] : '') * (isNaN(parseInt(this.form.elements['qty'].value)) ? 1 : parseInt(this.form.elements['qty'].value)));">
    And try:
    Code:
    <input name="total" type="text" onfocus="this.blur();" readonly="true">
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Apr 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for your reply.
    Sorry I dont think I made it clear to what I want.
    Basically any time you change the baskets and the qty I want to make the total field re-calculate. The price field will will only correspond to the price of the baskets. ie can only be:

    prices.toast = 40;
    prices.simple = 50;
    prices.fine = 60.5;
    prices.party = 112.34

    thanks
    Jason

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Oh, I'm sorry. Try:
    Code:
    <select name="baskets" onchange="this.form.elements['price'].value = (typeof prices[this.form.elements['baskets'].value] != 'undefined' ? prices[this.value] : '');this.form.elements['total'].value = ((typeof prices[this.value] != 'undefined' ? prices[this.value] : '') * (isNaN(parseInt(this.form.elements['qty'].value)) ? 1 : parseInt(this.form.elements['qty'].value)));">
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Apr 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thats great. Thanks for your help with this.

    Frogger

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •