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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Form

Status
Not open for further replies.

SH77

Technical User
Mar 20, 2007
3
US
Im trying to make a form that will submit different orders to paypal based on 3 radio options and 2 check boxes. I am passing one variable to paypal that may have 12 different values. How can I do this without having 12 radio options? Is javascript the only way? If javascript is disabled could the form default to one of the values?
 
Unless you have some server-side processing available to you to interpret the values from the radio and check boxes, then yes, you will need to use client-side scripting to aggregate those values.

The way that springs to mind is to include an [tt]onchange[/tt] event handler function to determine when the user makes a change to your radio-buttons or check boxes and build the possible value, then store that computed value in a hidden field on your form. Then submit that hidden field value to be processed.


Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Addendum... yes, if javascript was disabled you could still have a default value in the hidden field.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Thanks, but what I dont understand is how I use javascript to change the hidden value.
 
Code:
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
 <head>
  <script type="text/javascript">

  function recompileValue(){
  	var strValue = "radiobtn=";
  	var strButtonValue = "";
  	if(document.getElementById("radiobtn1").checked){
  		strButtonValue = document.getElementById("radiobtn1").value;
  	}
  	if(document.getElementById("radiobtn2").checked){
  		strButtonValue = document.getElementById("radiobtn2").value;
  	}
  	if(document.getElementById("radiobtn3").checked){
  		strButtonValue = document.getElementById("radiobtn3").value;
  	}
    strValue = strValue + strButtonValue;
    var strBoxValue = "";
    if(document.getElementById("cbox1").checked && document.getElementById("cbox2").checked){
    	strBoxValue = "both";
    }
    if(!document.getElementById("cbox1").checked && !document.getElementById("cbox2").checked){
    	strBoxValue = "none";
    }
    if(document.getElementById("cbox1").checked && !document.getElementById("cbox2").checked){
    	strBoxValue = document.getElementById("cbox1").value;
    }
    if(!document.getElementById("cbox1").checked && document.getElementById("cbox2").checked){
    	strBoxValue = document.getElementById("cbox2").value;
    }
    strValue = strValue + "&cbox=" + strBoxValue;
    document.getElementById("computedvalue").value = strValue;
    
    alert("The value of the 'computedvalue' field is now:\n\n" + document.getElementById("computedvalue").value);

  }
  </script>
 </head>
 <body>
  <form action="someform.ext" name="myform" id="myform">
  <fieldset>
  <legend>Radio Buttons</legend>
  <input type="radio" checked="checked" value="radio1" name="radiobtn" id="radiobtn1" onclick="recompileValue()"> radio1 <br>
  <input type="radio" value="radio2" name="radiobtn" id="radiobtn2" onclick="recompileValue()"> radio2 <br>
  <input type="radio" value="radio3" name="radiobtn" id="radiobtn3" onclick="recompileValue()"> radio3 <br>
  </fieldset>
  <fieldset>
  <legend>Check Boxes</legend>
  <input type="checkbox" value="cbox1" name="cbox" id="cbox1" onclick="recompileValue()"> cbox 1 <br>
  <input type="checkbox" value="cbox2" name="cbox" id="cbox2" onclick="recompileValue()"> cbox 2 <br>
  </fieldset>
  <input type="hidden" name="computedvalue" id="computedvalue" value="radiobtn=radio1&cbox=none">
  <input type="submit">
  </form>
 </body>
</html>

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Looking good, happy to have helped :)

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.

Enable Apps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top