Is there a way to call a PHP function via Javascript?
When a user clicks on a link, I need it to increment a PHP variable. Are any workarounds available?
Is there a way to call a PHP function via Javascript?
When a user clicks on a link, I need it to increment a PHP variable. Are any workarounds available?
I played around with xajax...its not what I need. I only want to increment a variable...
My javascript file is generated dynamically by php. I want a php variable to change after one of the javascript functions are called...
Alternately, I could use a javascript variable in my php, if that is easier. Basically, I have a php array whose index needs to be changed on click.
To clarify, I have a php variable named $index. The javascript should update $index, and then call the javascript functions unload() and load().
THE FOLLOWING MUST BE WRITTEN IN JAVASCRIPT
function update() {
$index++;
unload();
load();
}
If you need something to happen client side in order to interact with PHP (without reloading the page), then you need to use some form of AJAX. xajax will do that. example: This will take the value from a div 'myDiv', pass it to PHP, increment it by one, update the value in that div, and call the javascript functions unload() and load()
$xajax = new xajax();
// Register Functions
$xajax->registerFunction('myUpdate');
function myUpdate($index)
{
$objResponse = new xajaxResponse();
$objResponse->addClear('myDiv', 'innerHTML');
$objResponse->addAssign('myDiv', $index++);
$objResponse->addScript('unload()');
$objResponse->addScript('load()');
return $objResponse;
}
And you'd call this via javascript using
<a href="javascript:void(0);" onclick="xajax_myUpdate(document.getElementById('myDiv'));">Click Me to Lowercase String</a>
I'm not familiar with ajax without the xajax library, so I can't provide much help beyond that.
But you definitely need AJAX for what you're looking to accomplish.
I ended up just passing some URL parameters intead of onclick...its kind of nasty, but it works
why not create a 0px by 0px iframe that loads onclick? and that iframe page increments the variable?
ShawnK wrote:why not create a 0px by 0px iframe that loads onclick? and that iframe page increments the variable?
In other words, do what was done before Javascript got smart enough to do http itself?
But you don't need a heavyweight all-the-bells-and-whistles library to do Ajax interactions so long as you take the time to figure out what you're doing at all. See the link to Ledorf's tutorial on this page.