SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing variable from select box to hidden input

    Hi

    I'm trying to figure out how to pass a variable from a select box to a hidden input field in a form. With the slight added complication that I'd like to have a noscript alternative to stop sneaky types from just disabling JS.

    What I need is basically if x variable is chosen then the variable passed to the hidden field should be 10.00, else 5.00 - if that makes any sense.

    Here's what I've got so far JS wise (please try not to laugh!)

    Code:
    function taxcalc(){
    var ind = document.headset.shipping.selectedindex;
    var val = document.headset.shipping.options[ind].value;
    var taxamount = ""
    if (val == "15.00")
    {
     taxamount = "10.00";
     } else { 
     taxamount = "5.00";
     }
    And the form (simplified)

    HTML Code:
    <form action="whatever" id="headset" method="post">
    
    <label for="shipping">Select your shipping</label> 
    <select class="shipping" name="shipping" onchange="taxcalc (this.form);">
    	 <option value="5.66">Select your shipping</option>
    	 <option value="5.66">UK via Citylink &ndash; 5.66</option>
    	 <option value="15.00">Europe standard mail &ndash; 15.00</option>
    </select>
    	
    <input type="hidden"  id="tax" name="tax" value=""> 
    
    </form>
    Any help gratefully accepted!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project

  2. #2
    SitePoint Member GreboGuru's Avatar
    Join Date
    Jun 2006
    Location
    Wensleydale, UK
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The following should do what you want to do. I've made a slight change to the function, if the first option is selected then no tax is calculated.

    Code:
    function taxcalc()
    {
    	var e = document.getElementById("shipping");
    
    	var taxamount = "";
    	if (e.selectedIndex > 0)
    	{
    		var val = e.options[e.selectedIndex].value;
    	
    		if (val == "15.00")
    		{
    			taxamount = "10.00";
    		} 
    		else 
    		{ 
    			taxamount = "5.00";
    	 	}
     	}
     	
     	e = document.getElementById("tax");
     	e.value = taxamount;
    }

    I personally would do the tax calculation again on the server side to ensure that no one circumvents it by disabling JS ... never trust what gets sent to your server ... revalidate, recalculate ... always.

    Hope this helps
    Scott

  3. #3
    SitePoint Wizard bronze trophy Tailslide's Avatar
    Join Date
    Feb 2006
    Location
    Bedford, UK
    Posts
    1,687
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Scott

    I'll have a go tomorrow and see if I can get it working. I'd love to check the tax server-side, unfortunately it's Paypal!
    Little Blue Plane Web Design
    Blood, Sweat & Rust - A Land Rover restoration project


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
  •