Jump to content

[SOLVED] Set the value of a Hidden Field?


scooter41

Recommended Posts

Hi there, is it possible to dynamically set the value of a hidden field on a button? I guess via Javascript?

 

I was passing a prodID value on an image, which worked great in firefox, but then I found out in IE this doesnt work :( Its fine with a submit button+ value, but this looks ropey and not really what I am after!

 

Im basically trying to prevent having multiple forms with hidden ID's and just 1 form with the ID being set on the BUY ITEM button.

 

You can see the page here:

 

http://www.parts4windows.co.uk/product.php?pID=3490&catID=5

 

My new idea is to perhaps set the value of the hidden variable prodID dynamically when the user hits buy item.

 

Thanks for any help in advance!

Link to comment
Share on other sites

Yes you can do that, you don't need JavaScript.

 

<?php

$query = "SELECT prodID FROM table WHERE condition";
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)){
   echo "<input type='hidden' name='prodID' value='{$row['prodID']}'>";
}

?>

 

That was just a quick example on how to do it.

Link to comment
Share on other sites

I think missed the point poco - the particular product ID won't be known until the user clicks a particular button.

 

And yes, it'll be javascript to do this. You'll need to set up a function which will change the value of the hidden field depending on which button is pressed.

Link to comment
Share on other sites

found the answer, you can indeed refere to the form name as any other form element

 

so :

 

<script language="javascript">

function changeVar(changeTo) {

 

var property = document.formname.prodID;

property.value=changeTo;

alert (property.value); - to return the value to check

}

</script>

 

 

see here:

 

http://qa.techinterviews.com/q/20060729110146AAoy4lc

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.