PHP + Javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheWaterboy08
Forum Newbie
Posts: 4
Joined: Wed Oct 15, 2003 10:12 am

PHP + Javascript

Post 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>
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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>
TheWaterboy08
Forum Newbie
Posts: 4
Joined: Wed Oct 15, 2003 10:12 am

Post 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
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post 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.
Stoneguard
Forum Contributor
Posts: 101
Joined: Wed Aug 13, 2003 9:02 pm
Location: USA

Post 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.
Post Reply