Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
Similar answer on this question: stackoverflow.com/questions/10474259/… – Danny Apr 12 at 2:36

3 Answers

up vote 0 down vote accepted

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.

share|improve this answer
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 at 2:52
@hayonj Yeah, PHP runs before JavaScript. – Jack Apr 12 at 2: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 at 3:21
@hayonj What are page variables? – Jack Apr 12 at 3: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 at 3:30
show 3 more comments

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

share|improve this answer

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 "}";
share|improve this answer
i can make this code work. maybe some quotes are conflicting – hayonj Apr 12 at 3:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.