<?php $say=0 ?>

<script> 
for(var j=0;j<limit;j++)
          {
              tipi[j] = " <?php echo "$tipi[$say]" ?> ";
              gun[j] = " <?php echo "$gun[$say]" ?> ";
              ay[j] = " <?php echo "$ay[$say]" ?> ";
              yil[j] = " <?php echo "$yil[$say]" ?> ";

                    <?php $say++;  ?>
          }
<script/>

//I want to increase $say in javascipt. But not working. $say 's value is 0 everytime. What can I do?

Recommended Answers

All 3 Replies

PHP is processed by the server and JS as processed by the browser. By the time your javascript loop runs PHP has already been fully processed and you cannot modify PHP variables from within JS, you'll have to either do it entirely in JS or PHP not a combination of the two. What is it you're trying to accomplish?

<?php 
$tipi = array("asd","222","dda","xcs"); 
 $gun = array("qwe","vvv","zzz","bffg"); 
 $ay = array("asd","bbb","23a","wqe"); 
 $yil=array("zzz","sad","cxc","zxca"); 

$say=0 ?>

<script> 

var tipi = new Array();
  var gun = new Array(); 
  var ay = new Array();
  var yil = new Array();

for(var j=0;j<4;j++)
          {
              tipi[j] = " <?php echo "$tipi[$say]" ?> ";
              gun[j] = " <?php echo "$gun[$say]" ?> ";
              ay[j] = " <?php echo "$ay[$say]" ?> ";
              yil[j] = " <?php echo "$yil[$say]" ?> ";

                    <?php $say++;  ?>
          }
</script>

//I have arrays in php code. I want to transfer this values in javascript code.

Member Avatar for diafol

php variables can be tranferred to js no prob - as you've done.
js vars to php is more difficult - due to php running on server and js running on client - so you can use ajax to do this if you really need to.

However, php vars are lost as soon as the ajax call is done (just like a page call) - so unless you store data in sessions, cookies, DBs etc, it could be a lot of hard work without any benefit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.