0

When I click a button:

  1. I need the JavaScript to perform some function.
  2. Also run PHP code as well.

How can we achieve this?

Grokify
  • 15,092
  • 6
  • 60
  • 81
Abhinay Raj
  • 27
  • 10
  • 1
    multiple options, tried anything? –  Jun 11 '18 at 03:48
  • 2
    Possible duplicate of [How can I call PHP functions by JavaScript?](https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript) –  Jun 11 '18 at 03:51

6 Answers6

0

PHP is only run by the server and responds to requests like clicking on a link/button (GET) or submitting a form (POST).

HTML & JavaScript is only run in someone's browser.

<html>
<?php
  function PhpFunction() {
    echo 'A php function';
  }

  if (isset($_GET['JsFunction'])) {
    PhpFunction();
  }
?>

Hello there!
<a href='index.php?JsFunction=true'>Run PHP Function On Click Of This Link</a>
</html>
Sanjaysinh Zala
  • 134
  • 2
  • 12
0

Alternatively , You can create a ajax request to run a php code in your server..

0

Ajax can help you to send an asynchronous request to the server that php (or other) can catch, in this way you can implement and play with some callback functions

$.ajax({
       url : "yourScript.php", // the resource where youre request will go throw
       type : "POST", // HTTP verb
       data : { action: 'myActionToGetHits', param2 : myVar2 },
       dataType: "json",
       success : function (response) {
          //in your case, you should return from the php method some fomated data that you  //need throw the data var object in param
             data = toJson(response) // optional
            //heres your code

       },
       error : //some code, 
       complete : //...
});

in your php script, you'll receive the request posted throw the superglobal vars like POST (for this example)

<?php

$action = (string)$_POST['action']; //this is unsecure, its just for the example

if("myActionToGetHits" == $action) {

 //here you have to call your php function and so on..
$data = hitsMonth();
echo $data;
exit;
}

here is html

<a href="#" onclick="javascript:functionName(arg1, arg2);">

This is a basic example to do it, there are lots of ways to do.

munsifali
  • 1,732
  • 2
  • 24
  • 43
0

Have javascript do the submit:

function button1() {
  // js code here
  var formelt = document.getElementById("form1");
  formelt.submit(); // this will submit the form to server
}
dcromley
  • 1,373
  • 1
  • 8
  • 23
0

Example =>

<button class="btn-test">Btn Test</button>

<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
    jQuery('.btn-test').click(function() {
        js_do_something();
        php_do_something();
    });

    function js_do_something() {
       console.log('something done from js');
    }

    function php_do_something() {
        console.log('something done from php');
        jQuery.ajax({
            type: "POST",
            url: 'you_php_file.php',
            data: {param: 'btn-test-click'},
            success: function () {
                console.log('success');
            }
        });
    }
</script>
WHY
  • 258
  • 1
  • 8
0

There you have how to execute a php function with on Onclick.

Execute PHP function with onClick

You can execute a Javascript function assuming you´re using jQuery like this:

jQuery('#id-button').on('click', function(){
    // your function body
});
sanNeck
  • 148
  • 7