Page 1 of 1

Incrementing a php variable in javascript

Posted: Sun Oct 12, 2003 7:26 am
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.

Posted: Sun Oct 12, 2003 7:31 am
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>).

Posted: Sun Oct 12, 2003 7:32 am
by Judas
no way

Posted: Sun Oct 12, 2003 7:45 am
by JAM

Posted: Sun Oct 12, 2003 1:42 pm
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