PDA

View Full Version : Multiple Values in one form field?


chornbeck
09-11-2006, 03:34 PM
I need to have a form field that holds two values instead of one to get the following accomplished:

Let's say I'm selling blue, red, and green widgets. I have a radio button that chooses which one you want to purchase. What I need to happen, is when the form posts to a PHP script, is to have a variable available for both the name of the product chosen, as well as the price of that product.

Kind of like: ( I know this won't work, but it illustrates my point)

<input type="radio" name="widgets" value1="Blue Widget" value2="$50">BLUE WIDGETS</input>

<input type="radio" name="widgets" value1="Green Widget" value2="$75">GREEN WIDGETS</input>

<input type="radio" name="widgets" value1="Red Widget" value2="$25">RED WIDGETS</input>


Any suggestions are greatly appreciated!

Mr J
09-11-2006, 06:58 PM
Would something like this be to your liking.
Place the 2 values as a single value seperated by a comma then use the split() method to retrieve each value

<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<script type="text/javascript">

function getValues(){

myForm=document.forms["myform"]

for(var i=0;i<myForm.length;i++){

if(myForm.elements[i].type=="radio"&&myForm.elements[i].checked){
temp=myForm.elements[i].value.split(",")
}


}

document.getElementById("testdiv").innerHTML="Colour = "+temp[0]+"<br>Price = "+temp[1]
}


</script>

</HEAD>
<BODY>

<FORM name="myform">

<input type="radio" name="widgets" value="Blue Widget,$50" onclick="getValues()">BLUE WIDGETS</input>

<input type="radio" name="widgets" value="Green Widget,$75" onclick="getValues()">GREEN WIDGETS</input>

<input type="radio" name="widgets" value="Red Widget,$25" onclick="getValues()">RED WIDGETS</input>


</FORM>

<div id="testdiv"></div>

</BODY>
</HTML>

mark87
09-11-2006, 07:35 PM
Wouldn't it be easier to specify the value of the items in the PHP script? That way, (obviously depending on how it's set up), users won't be able to change prices themselves(!), and those with JavaScript disabled could still buy the items.