-2

I need to call a PHP function as simply as possible with OnClick event:

<button onclick="Function(5)">Call PHP function</button>

I searched for the answer, but could not find it. I think I have to use AJAX or something, but I have no experience with it. Please give me an example as simple as possible on how to call a PHP function with parameter using OnClick event.

ZygD
  • 22,092
  • 39
  • 79
  • 102
Doubstract
  • 1
  • 1
  • 5

2 Answers2

0

I wouldn't use inline events on html elements - it becomes a bit of a mess. You are going to need to use Ajax to get this done. Firstly i would set up your php function in it's own page/action then set up your javascript/jquery request event like so.

<button class="call-php" value="click me"/>

$('.call-php').click(function(){
    $.ajax({
            async: false,
            url  : '/myphp.php', 
            type : 'get',
            data : {},
            success:function(data) {
                // do something
            }, 
        });
});
John williams
  • 654
  • 1
  • 8
  • 22
0

You can call like this

<?php Function(5)?>)
arulmr
  • 8,620
  • 9
  • 54
  • 69
Muhammad Waqas
  • 1,140
  • 12
  • 20