Page 1 of 1
how to retrieve a javascritp value in php?
Posted: Mon Dec 05, 2005 9:05 am
by taha
Ok on the JavaScript side i have a variable that is called 'partNoValues' that stored the values of partNo. for that variable i have a get/set function for convenience, a function to set the variable and a function to get the variable value...see below:
Code: Select all
function setPartNoValues(pnoValues)
{
partnoValues = pnoValues;
}
function getPartNoValues()
{
return partnoValues;
}
i have a button called save on my form.
now on the php side, i have a catch for when the save button is pressed
in this loop i need to take the values from the 'partNoValues' variable and store them into mysql database.
how can i access the javascript variable in php is the question???
Posted: Mon Dec 05, 2005 9:19 am
by foobar
You'll need a form with all the part numbers in it. This doesn't really have anything to do with PHP, though. What you need to do is in setPartNoValues(), is set a hidden form field's value to pnoValues. Once this is done, the user posts the form by clicking the button. Like so:
Code: Select all
<!-- some stuff -->
<script type="text/javascript">
function setPartNoValues(pnoValues)
{
partnoValues = pnoValues;
/* set field value */
document.theform.thefield.value = pnoValues;
}
function getPartNoValues()
{
return partnoValues;
}
</script>
<!-- some more stuff -->
<form name="theform" action="somepage.php" method="post">
<input type="hidden" name="thefield" value="" />
<input type="Submit" value="Save" />
</form>
Your php file:
Code: Select all
if (isset($_POST['thefield'])) {
//do some crazy stuff
}
[edit] Code altered to be applicable to php.
Posted: Mon Dec 05, 2005 9:48 am
by taha
Thanks soo much foobar!
Posted: Mon Dec 05, 2005 9:56 am
by foobar
taha wrote:Thanks soo much foobar!
Hey, no problem, pal.
I know it can get confusing when you're trying to bridge what you see in your browser with what php is supposed to do with it. Always remember to distinguish between what's happening on the client side, and on the server side. When I started out with ASP many moons ago (must've been like 12 then), I was overwhelmed by the whole client-side vs. server-side thing. However, by reading literature that was much to difficult for me, the notion forced itself into my brain. It was painful, but effective.
Here's a Google search that might help you grasp the difference:
http://www.google.com/search?q=differen ... erver-side
That's not to say that you don't "get it", but you may not be fully aware of the implications of it. It's something you have to grasp. Don't worry if it doesn't make a whole lot of sense now. Practice until it does. It will make sense after a while, promise!
