Incrementing a php variable in javascript

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
guardian
Forum Newbie
Posts: 1
Joined: Sun Oct 12, 2003 7:26 am

Incrementing a php variable in javascript

Post by guardian »

Hi,
I am in need of a fix... I'm am running mainly PHP but in some cases need to have javascript for manipulation of client side data. The problem i'm having is the following:

When a page gets loaded i'm building PHP arrays. These arrays and variables are global. Depending on what the user selects, I am searching through the PHP array. The problem is that I can't seem to increment a PHP variable within the javascript code. The following code shows the problem..... here I am trying to increment $GLOBALINC as I use it as an index in the array.

function blah()
{
var cnt = <?=$cnt ?>;
var ind = 0;
var rr = "";
while(ind < cnt)
{
<?php
if($cnt > 1)
{
?>

rr = '<?= $rrconfig[$GLOBALINC][0] ?>';

<?php
$GLOBALINC++;
}
?>
ind++;
}

Any ideas why the increment won't happen?

Thanks.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Because when the page ends up at the client, the PHP is allready generated. Javascript only works on the browser, PHP only works on the server. You cannot ccombine these two like this.

If you need to manipulate data based on userinput, you need to send it to the server for PHP parsing using something like $_GET (http://url.com/page.php?integer=1) or $_POST (using a <form>).
Judas
Forum Commoner
Posts: 67
Joined: Tue Jun 10, 2003 3:34 pm
Location: Netherlands

Post by Judas »

no way
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

if you move the items causing the issue to get then you can utelyze javascript to make the variable changes in the pae the form calls (if using a form that uses get, if usingone with post make post affect it)


remember server side finishes before anything happens client side. the only way clientside can affect server side is by calling the serverside again
Post Reply