Jump to content

php onclick


blic

Recommended Posts

Hi All,

 

I wanted to have in a page a link (or button)  that, when clicked, called a function that would change some php variables. The piece of the the code would be someething like this:

 

echo "<a href='#' onclick='foo()'><b>call foo</b></a>";
?>
<script>
foo(){
<?
$la = new MyClass();
$la->addver($ver);
?>
}
</script>
<?

 

I know this is not correct, so I was wondering if anybody has  a simple solution to that.

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/238887-php-onclick/
Share on other sites

Since PHP is server side then your link would need to call the same page and pass a parameter telling what function to call.  This is just a rough example:

 

echo "<a href='{$_SERVER['PHP_SELF']}?func=foo'><b>call foo</b></a>";

$safe_funcs = array('foo', 'bar');

if(isset($_GET['func']) && in_array($_GET['func'], $safe_funcs)) {
   $_GET['func']();
}

function foo(){
   $la = new MyClass();
   $la->addver($ver);
}

Link to comment
https://forums.phpfreaks.com/topic/238887-php-onclick/#findComment-1227486
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.