user20118994
user20118994

Reputation:

How to call php code from button onclick with Ajax?

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';

Upvotes: 0

Views: 140

Answers (3)

user20118994
user20118994

Reputation:

Here is how I finally did it :

I gived an id to my button:

<button id="<?php echo $rows['id']; ?>" onclick ="deletedata(this.id)">Delete</button>

I give in deletedata the parameter this.id, it's a way to give the id of the button as parameter, then I use Ajax to call delete:

<script type="text/javascript">
        // Function
            function deletedata(id){
                   $.ajax({
                        // Action
                        url: 'admin',
                        // Method
                        type: 'POST',
                        data: {
                        // Get value
                        id: id,
                        action: "delete"
                        },
                        success:function(response){
                        }
                    });
                };
    </script>

Here is the tricky thing, I didn't use a fonction as I thought I needed. Instead I did this :

 if (isset($_POST["action"])) {
            echo "Hello the World! ";
            // Choose a function depends on value of $_POST["action"]
            if($_POST["action"] == "delete"){
                mysqli_query($conn, "DELETE FROM bdd_sites WHERE id = " . $_POST['id'].";");
            }
            header('Location: '.$_SERVER['REQUEST_URI']);
          }
        ?>

Upvotes: 0

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.

Upvotes: 0

Lucas
Lucas

Reputation: 452

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>

Upvotes: 0

Related Questions