2

Section 1

I'm trying to return an Array from PHP (after having created the file using fwrite)

I include that file on my next .load method (inside a Div) the new PHP file contains an include 'somefile.php'; there is my array ... and I would simply try to collect from PHP and use it the array with JS....

I once saw something like this ...

$(function(){
alert('<?php for_each($array as $key => $value){
         echo $value;     // Just an example
}
?>')
});

I wrote this piece of code on the fly so there might be a few sytax errors;

This works fine using PHP inside JS ...

I'm not sure if I heard PHP loads first and then JS ? or was it the other way around ?

Section 2

How about using JS inside PHP ? for example ....

<?php

echo "var myArray = New Array();"
echo "myArray = ('Apple','Banana','Orange','Kiwis');"
echo "return myArray";

?>

and then being able to fetch the data strait with JS ?

<script type="text/javascript">
    for(i=0;i<myArray.length;i++){
       alert(myArray[i]);
</script>

So how easy can I manipulate both languages in regards to RETURNING the array for example so it could be used in the global scope?

Ian
  • 50,146
  • 13
  • 101
  • 111
hayonj
  • 1,429
  • 3
  • 21
  • 28
  • Similar answer on this question: http://stackoverflow.com/questions/10474259/how-can-i-return-array-from-php-to-javascript-using-ajax – Danny Apr 12 '13 at 02:36

3 Answers3

0

using PHP in JS : In short, you can echo the values in JS codes, e.g.

window.location = '<?php echo $url; ?>';

using JS in PHP : You can use AJAX . post the values to a PHP script

Raptor
  • 53,206
  • 45
  • 230
  • 366
0

PHP runs first, so you can use it to write JavaScript code, that will get run once the page has been processed by the browser.

This would build JavaScript code with an array from PHP:

<script>
var myArray = <?php echo json_encode(array('apple', 'orange', 'kiwi'); ?>;

myArray.forEach(alert)
</script>

The other way around, passing data from JavaScript to PHP, can only be accomplished with some form of AJAX.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • values have to be posted absolutely in order to see them on the PHP side ? is there no other way ? But using Ajax ? – hayonj Apr 12 '13 at 02:52
  • @hayonj Yeah, PHP runs *before* JavaScript. – Ja͢ck Apr 12 '13 at 02:52
  • that's pretty cool the code works ... and here I get to learn 1 PHP function and 1 JS method ... Is it possible to view all the page variables either with PHP or JS in firebug ... I know if you post you could see them in the NET panel under parameters and actual DOM Inspector is a little crazy it goes too many levels deep ... But it would be nice to see my current page variables – hayonj Apr 12 '13 at 03:21
  • @hayonj What are page variables? – Ja͢ck Apr 12 '13 at 03:22
  • i dont know ... say i right a bunch of `var apple = "red"; var banana = "yellow"; var fruits = array('kiwi','lemon','grape');` Where can i see the variables that were called from all my scripts included in the page using firebug .. ? – hayonj Apr 12 '13 at 03:30
  • @hayonj Well, it's easier to put them into a namespace container, e.g. `var fruits = { apple: 'red', banana: 'yellow' }`, so then `window.fruits` will contain your ... fruits :) – Ja͢ck Apr 12 '13 at 03:33
  • @hayonj Namespaces can be seen as a generic term to group things together :) – Ja͢ck Apr 12 '13 at 03:47
0

The PHP runs on the server, so from Javascript's point-of-view (running in the client's browser) it's like the PHP was never even there.

You can easily pass the PHP array to Javascript using PHP's json_encode() function:

$yourPHPArray = array("blah","blah","blah");

echo "var theArray=" . json_encode($yourPHPArray) . ";";

echo "for(var i=0;i<theArray.length;i++)";
echo "{";
echo "     window.alert(theArray[i]);";
echo "}";
Alex W
  • 37,233
  • 13
  • 109
  • 109