Page 1 of 1

PHP + Javascript

Posted: Wed Oct 15, 2003 10:12 am
by TheWaterboy08
Im a bit stuck here. I am trying to get the value from a JavaScript function and place it in to the VALUE of a HIDDEN field. Does anybody know how to do this. An extract of my code is below:


<SCRIPT LANGUAGE="JavaScript">
function popItUp() {
//creating a new window and loading it with contact.html
var newWindow = open("shipping.html", "secondWindow", "width=300,height=400");
}

function ChangeItem(lens,frame,price) {
document.ProductForm.Detail1.value = lens
document.ProductForm.Detail2.value = frame
document.ProductForm.Price.value = price
document.ProductForm.BasePrice.value = price

}
</script>
</head>

<div align="center">
<form name='ProductForm' method='post' action='test2.php'>
<input type="radio" name="LenFrameID" value="16478" onClick="ChangeItem('Black Violet','Black','84.95')"
CHECKED>
<br>
<input type="radio" name="LenFrameID" value="16479" onClick="ChangeItem('Violet','Blue','88.95')">

<input type="hidden" name="BasePrice" value="HOW DO I GET VALUE OF BASEPRICE HERE">
<input type='submit' value='Continue'>
</tr>
</form>

Posted: Wed Oct 15, 2003 10:18 am
by Stoneguard
Are you wanting to double check it is working? The code looks fine, although I typically don't use the form name. Just add an alert to give you an idea if the value is set:

Code: Select all

<SCRIPT LANGUAGE="JavaScript"> 
function popItUp() &#123; 
   //creating a new window and loading it with contact.html 
   var newWindow = open("shipping.html", "secondWindow", "width=300,height=400"); 
&#125; 

function ChangeItem(lens,frame,price) &#123; 
   var fm = document.ProductForm;
   fm.Detail1.value = lens 
   fm.Detail2.value = frame 
   fm.Price.value = price 
   fm.BasePrice.value = price 

   alert(fm.BasePrice.value);
&#125; 
</script> 
</head> 

<div align="center"> 
<form name='ProductForm' method='post' action='test2.php'> 
<input type="radio" name="LenFrameID" value="16478" onClick="ChangeItem('Black Violet','Black','84.95')" 
CHECKED> 
<br> 
<input type="radio" name="LenFrameID" value="16479" onClick="ChangeItem('Violet','Blue','88.95')"> 

<input type="hidden" name="BasePrice" value="HOW DO I GET VALUE OF BASEPRICE HERE"> 
<input type='submit' value='Continue'> 
</tr> 
</form>

Posted: Wed Oct 15, 2003 10:29 am
by TheWaterboy08
the code is okay. My problem is that I cannot put the value that is set in the price variable on the CahngeItem function into the hidden input value field as I dont know how. The reason is that for each checkbox there is a differnet price. If a checkbox is clicked I need the price so the variable is set each time. How do i get this variable into a hidden field

Posted: Wed Oct 15, 2003 10:33 am
by choppsta
From what I can see it's already doing what you want...

Each time ChangeItem is called the line...

document.ProductForm.BasePrice.value = price

Sets the value of the hidden field BasePrice to the value of price.

Posted: Wed Oct 15, 2003 10:44 am
by Stoneguard
You can pass in the fld itself to easily get teh alue, or you can loop through all the fields in the array to check which one is checked.

I recommend something like this:

Code: Select all

...
function ChangeItem(lens,frame,price, fld) &#123; 
   var fm = document.ProductForm; 
   fm.Detail1.value = lens 
   fm.Detail2.value = frame 
   fm.Price.value = price 
   fm.BasePrice.value = fld.value 

   alert(fm.BasePrice.value); 
&#125; 
</script> 

...

<input type="radio" name="LenFrameID" value="16478" onClick="ChangeItem('Black Violet','Black','84.95', this)" 

...
note the added parameter called 'this'. That parameter actually returns the exact control in which it sits, so 'fld.value' in the function will get 16478 for the row above.