Manipulating arrays in php and javascript: best way forward?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sjk1000
Forum Commoner
Posts: 26
Joined: Tue Nov 11, 2008 8:50 am

Manipulating arrays in php and javascript: best way forward?

Post by sjk1000 »

Hi all
I've been stumped over this for days and days now. I have an array in php that I want to perform calculations on via the user clicking radio buttons. The results should be shown instantly on the page. The radio buttons have several values associated with them that will be used in the calculations when the button is clicked.

I've read about hiding the array as a document object so that js can get to it, which I've no experience of doing yet, and I've seen ways of passing the whole array to javascript and then back before the page is submitted, which apparently isn't straightforward. I don't have the experience to say which, if any, of these will work.

To summarise (just in case I've baffled everyone :) ), I'm trying to display results of instant calculations on values in a php array following the click of a radio button. What's the best method out there for this?

Any help is very much appreciated. This has driven me more nuts than anything I've done so far. I may go mad soon and I'll take u all with me. :)
Thanks , Steve
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Re: Manipulating arrays in php and javascript: best way forward?

Post by Burrito »

you can build the array on the client side by just ouputting a string from php or you can use AJAX and JSON to pass a 'serialized' array back and forth between the two.

google those and post back if you need more help.
tanmay
Forum Newbie
Posts: 12
Joined: Mon Jan 19, 2009 9:51 am

Re: Manipulating arrays in php and javascript: best way forward?

Post by tanmay »

hi steve, do 1 thing

in d page where you have html code:
first.php:

Code: Select all

<script type="text/javascript">
funtion arr()
{
   var arr1=new array();
   if(document.getElementById("radio").checked==true)
  {
      arr1[0]=document.getElementById("radio").value;
      //do watever you want with the values
  }
  form.submit();   
}
</script>
<body>
<form action="first.php" type="submit" method="post">
<input type:"radio" name="radio" id="radio" onclick="arr()">
</form>
<?php
// you can use the array here
echo $_POST['arr'];
?>
</body>
Post Reply