Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
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 Mar 16 '11 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 Mar 16 '11 at 19:57
add comment

5 Answers

up vote 0 down vote accepted

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).

share|improve this answer
add comment

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 computer.

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

You need to increment the variable before you print it:

<?php echo ++$k; ?>
share|improve this answer
1  
Perhaps, but I think you missed the key problem here ;-) –  Carpetsmoker Mar 16 '11 at 19:57
    
I intentionally overlooked the mixing PHP with JS and just offered a solution to the raw problem. :) –  smottt Mar 16 '11 at 19:59
add comment

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>
share|improve this answer
add comment

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.