how to retrieve a javascritp value in php?

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
taha
Forum Newbie
Posts: 17
Joined: Tue Nov 22, 2005 2:40 pm

how to retrieve a javascritp value in php?

Post 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

Code: Select all

if(isset($_POST['savebutton']))
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???
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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.
taha
Forum Newbie
Posts: 17
Joined: Tue Nov 22, 2005 2:40 pm

Post by taha »

Thanks soo much foobar!
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post 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! :wink:
Post Reply