最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

increment php variable in javascript function - Stack Overflow

programmeradmin1浏览0评论

Can anyone tell me how to increment a php variable inside a javascript function.I have a javascript function which is called for every 3 seconds inside this function i need to increment the php variable.

<? php $k =0?>
function slide()
{
 //increment $k
 var j =<? php echo $k++; ?>//when I do this k is not incremented
}

Can anyone tell me where I went wrong?Is their any other alternative to do this?

Thanks,

Shruti

Can anyone tell me how to increment a php variable inside a javascript function.I have a javascript function which is called for every 3 seconds inside this function i need to increment the php variable.

<? php $k =0?>
function slide()
{
 //increment $k
 var j =<? php echo $k++; ?>//when I do this k is not incremented
}

Can anyone tell me where I went wrong?Is their any other alternative to do this?

Thanks,

Shruti

Share Improve this question asked Mar 16, 2011 at 19:53 ShrutiShruti 7814 gold badges14 silver badges25 bronze badges 2
  • 1 That's definitely not going to work because by the time the JS is doing it's thing (client-side), PHP is already done (server-side). What are you trying to achieve overall? – Marcel Commented Mar 16, 2011 at 19:56
  • 1 $k++ says use $k in the current operation, and then increment it. So in the outputted JS you would see j being set to 0. To have j set to 1, You could do ++$k which would increment $k, then use it in the current operation. – JAAulde Commented Mar 16, 2011 at 19:57
Add a ment  | 

6 Answers 6

Reset to default 2

Short answer: you don't.

Longer answer: PHP runs and only when the page is first loaded. This code is executed on the server. Javascript on the other hand is executed after the page has finished loading, inside the browser on the client's puter.

Maybe someone can give some advice if you tell us what exactly are you trying to acplish.

PHP is run on the Server, Javascript is client side. What I think you want is a way to increment the JS value using Javascript not PHP.

The users never see php, so there is no incrementation happening.

You need to have a javascript function (clock) that keeps track of your 3 seconds, and then increase the value of your javascript var j every elapsed period (if I understood your questions correctly).

PHP is serverside, Javascript is clientside. You can't mix apples and oranges and expect bananas. Two things work in a different way. Use JS if you need JS variable incremented.

You need to increment the variable before you print it:

<?php echo ++$k; ?>

You can return k from php and increment it in Javascript. Have your code like this:

<script>
<?php $k=5; echo "var k=$k;\n";?>
function slide()
{
 //increment k
 var j = k++; //k is incremented ONLY in javascript and j get's the k's value
}
</script>
//You can do something like this:
<?php 
$options= array('option1', 'option2','option3','option4');
?>

/* in the same file you can convert an array php into an array javascript with json_encode() php function*/

<script>

/*In this way you can read all the javascript array from php array*/

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

/*And you can increase javascript variables. For example:*/

    for(var i =0; i<options_javascript.length; i++ ){
        options_javascript[i];
    }

</script>
发布评论

评论列表(0)

  1. 暂无评论