Home > Mobile >  How to call php code from button onclick with Ajax?
How to call php code from button onclick with Ajax?

Time:09-29

I'm new into php and I am trying to call code from another file. I try to use ajax to so, because later I would like to add parameters. But unfortunattely for me nothing appen when I click on my button.

I have a button in my file admin.php that is written like this:

<button onclick="clickMe()"> Click </button>

And in the same file I have my ajax code in script balise:

    <script>
                function clickMe() {
                    $.ajax( {
                        url: 'delete.php',
                        type: "POST",
                        success: test() {
                            alert('ok');
                        }
                        error : test(){
                                alert("error");                                  
                            }
                    });
                }
</script>

And here is the code that I'm trying to call in my ajax, the function test in the file delete.php:

<?php

function test() {
    echo "Hello the World! ";
}

?>

I wondering if I maybe need to put the code in delete.php in a function ?

Do you think I need to post the entirety of my admin.php file, even thought a lot of the code is not related to the question ?

EDIT: I forgot to mention; i have require delete file in my admin one:

require 'delete.php';

CodePudding user response:

I don't know jQuery, but I think your code should look something like this:

<?php
// delete.php
// make somthing
return 'Helo Word';
<script>
function clickMe() {
    $.ajax( {
        url: 'delete.php',
        type: "POST",
        success: response => {
            alert(reponse);
        },
        error: error => {
            alert(error);
        }
    });
}
</script>

CodePudding user response:

let's assume that your js code is working(i'm bad with JQuery). The JS code and the PHP code are living in different worlds but can connect by HTTP requests(XML-AJAX) and some others.

You can do a request to a PHP page like my-domain.com/the-page.php?get_param_1=value(GET method), and you can pass the same params(and a little more) by POST method. GET and POST params are looking like : param_name=param_value&param_name=param_value&param_name=param_value

You can't call directly PHP function(like var_dump('123);), but you can do this request with JS my-domain.com/the-page.php?call_func=myFunc123&printIt=HelloMate

to php page

<?php

function myFunc123($printText) { echo $printText; }

if (array_key_exists('call_func', $_GET)) {
   
   $param_callFunc = $_GET['call_func'];
   
   if ($param_callFunc == 'myFunc123') { myFunc123($_GET['printIt']); }
   
}

?>

Yes, you can pass any existing function name and call it, but it's not safe in future usage. Above, i use "page" word because you should do a request, not php file read or access.

CodePudding user response:

you should try calling test() function, just do something like that:

<?php

function test() {
    echo "Hello the World! ";
}

test();

?>
  • Related