1

The code is like this:

<SCRIPT LANGUAGE="JavaScript">
     function showReview(){
      //javascript stuff
         <?php
            $http="obj.href ='http://localhost/PROJECT1/thispage.php'"; 
               if (array_key_exists(0, $arr)){
            $http .= "+'&PQID={$arr[0]['ID']}'+
                     '&PQNo={$arr[0]['QNo']}'+
                     '&PNextSWF={$arr[0]['NextSWF']}';";
            }
            echo $http; 
           ?>

        }
</SCRIPT>

But I can't access $arr array. I tried to declare it global or use the $GLOBALS variable.

Show Review is called during onclick. $arr is set in the main php code. I tried just accessing the array in the main php code and passing the resulting string to the javascript which is the '?&PQID=ar&PQno=1...' part of the URL but it doesn't pass successfully. I tried passing the array itself to the javascript but js but I couldn't access the contents.

4
  • JavaScript, a client-side technology, will never be able to access data from PHP, a server-side technology. At best, what you can do, is use PHP to generate JavaScript code that will then be executed by your visitors' browsers. Commented Mar 4, 2011 at 7:12
  • @madd0 That is not entirely correct, depending on your definition of "accessing data." Even after a page is loaded, you could send an AJAX request to a PHP script that responds with data from the server. I would call that "accessing data" from PHP. Commented Mar 4, 2011 at 7:18
  • @NullUserException: the data you are talking about was most likely marshaled into JSON (i.e. JavaScript) by the PHP script that you called in order for the JavaScript to interpret, so no, JavaScript is not accessing PHP data directly. Commented Mar 4, 2011 at 7:22
  • @madd0 Of course not; that's why I said it depends on your definition of "accessing data." Commented Mar 4, 2011 at 7:31

3 Answers 3

3

PHP runs on the server, Javascript on the client - they can't see each other's variables at all really. Think of it this way - the PHP code just generates text. It might be Javascript, but as far as the PHP concerned, it's just text.

Basically, you need to use PHP to generate text which is valid Javascript for creating the same data structure on the client.

Sign up to request clarification or add additional context in comments.

Comments

1

Add this to the JS-function:

var arr=<?php echo json_encode($arr); ?>;

The PHP-Array "$arr" should now be accessible to JS via "arr" inside the JS-function.

I guess you are trying something like this:

<?php
  //example array
  $arr=array(
    array('ID'=>'0','QNo'=>'q0','NextSWF'=>1),
    array('ID'=>'1','QNo'=>'q1','NextSWF'=>2),
    array('ID'=>'2','QNo'=>'q2','NextSWF'=>3),
  );
?>
<script type="text/javascript">
function showReview(nr)
{
  //make the array accessible to JS
  <?php echo 'var arr='.json_encode($arr);?>

  //some obj, don't no what it is in your case 
  var obj={};

  var href='http://localhost/PROJECT1/thispage.php';
  if(typeof arr[nr]!='undefined')
  {
    href+='?PQID='+arr[nr]['ID']+
          '&PQNo='+arr[nr]['QNo']+
          '&PNextSWF='+arr[nr]['NextSWF'];
  }
  else
  {
    alert('key['+nr+'] does not exist');
  }

  //check it
  alert(href);

  //assign it
  obj.href=href;
}


</script>
<b onclick="showReview(0)">0</b>-
<b onclick="showReview(1)">1</b>-
<b onclick="showReview(2)">2</b>-
<b onclick="showReview(3)">3</b>

4 Comments

I tried that. I can't access the contents. Perhaps because js doesn't support associative arrays?
associative arrays are supported too by json_encode. But I don't see where you try to access this array via javascript, can you please explain how and where it happens?
Really? Then I dunno why it's not showing. After that code I tried alert(arr[0]['ID']); and alert(arr.length);. Nothing shows.
Took me so long to figure out why your example works but my code doesn't. Turns out I had to transfer my javascript below my main php code, now it works just fine. So thanks for your help.
1

Try this

<SCRIPT LANGUAGE="JavaScript">
     function showReview(){
      //javascript stuff
      var http = 
         <?php
            $http="obj.href ='http://localhost/PROJECT1/thispage.php'"; 
               if (array_key_exists(0, $arr)){
            $http .= "+'&PQID={$arr[0]['ID']}'+
                     '&PQNo={$arr[0]['QNo']}'+
                     '&PNextSWF={$arr[0]['NextSWF']}';";
            }
            echo $http; 
           ?>

        }
</SCRIPT>

4 Comments

did you try doing a "view source" to check what is being rendered in the browser ?
I cut the 'obj.href=' part and tried to document.write the http var, it doesn't display anything.
and what about the array $arr , does it exist ?
Yes it does and I can assign new values to it..but the values which were assigned in the main php part are gone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.