Run php function on button click using ajax

Before someone has a go at me or marks this down, I have looked all over the internet to find out how to do this [including the same question on stackoverflow]. I'm new, and I am finding it very hard to learn new concepts so please be easy on me.

What I want to do is call a php script/function on a button click. I have this running in WAMP if that helps. Here's my code:



Click Me

the_script.php has this in it:

the_function[] {
    echo "You win";
}

Why isn't this working? I've heard about the button being client side etc. and PHP being server-side, which means that you cannot link the two together. I know that you have to use AJAX to make this work, however I legitimately have absolutely no clue how to do it. I've tried googling it etc., however I can't find anything. I know how to use AJAX and call events with it, however I still have no idea how to make it call a PHP script.

Can you please make your answers as clear and simple as possible, I'm new to this

Thanks for the help :]

EDIT ***

For some reason wherever I go everyone's code is different. The way I have been taught AJAX looks completely different. Can you please write it this way so I can understand? Thanks, here's an example:

var request;

if [window.XMLHttpRequest] {
    request = new XMLHttpRequest[];
} else {
    request = new ActiveXObject["Microsoft.XMLHTTP"];
}

request.open['GET', 'file.php', true];

request.onreadystatechange = function[] {

    if [request.readyState===4 && request.status===200] {
        do stuff
    }
}

request.send[];

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a document containing HTML and PHP code and the task is to call the PHP function after clicking on the button. There are various methods to solve this problem. Also, apart from doing this with the click of a button, a PHP function can be called using Ajax, JavaScript, and JQuery. But this article mainly focuses on the button oriented approach of calling the PHP Function.

    Calling a PHP function using the HTML button: Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists[] function called.

    Program 1:

        

            How to call PHP function

            on the click of a Button ?

        

        

            GeeksforGeeks

        

        

            How to call PHP function

            on the click of a Button ?

        

        

        

            

            

        

    Output:

    Let’s take a look with the GET or POST methods, as most developers use POST method due to privacy issues, the following example is based on POST method only:

    Program 2: This program uses isset[] function to call PHP function.

        

            How to call PHP function

            on the click of a Button ?

        

        

            GeeksforGeeks

        

        

            How to call PHP function

            on the click of a Button ?

        

        

        

            

            

        

    Output:


    Chủ Đề