Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.