user444434
user444434

Reputation: 51

Sending A POST or GET request to php script using javascript

Here is what needs to be done.

I have an html code and when use clicks into input field the javascript function is called.

The javascript function should call php file track.php which then counts the clicks.

Is it possible to run a php script from javascript?

Upvotes: 2

Views: 2982

Answers (3)

Al W
Al W

Reputation: 7713

Now would be as good a time as any to learn jQuery

Upvotes: 1

o01
o01

Reputation: 5440

Try using the jQuery framework for your javascript code. And have a look at the ajax function.

$.ajax({
        type: "POST",
        url:  "track.php",
        data: form_data,
        success: function(){
            // code
        },
        error: function(){
            // code
        }
    });

Upvotes: 7

Pascal MARTIN
Pascal MARTIN

Reputation: 400932

Yes, it is possible : you'll want to search for the "Ajax" keyword.

Ajax will allow you to send requests, from Javascript to your server, in the background -- without reloading the page.


If you just want to send a simple request, working with the XmlHttpRequest object that's provided by the browser is quite easy.

Else, if you want more advanced stuff, you'll probably want to take a look at Javascript libraries, such as jQuery.

Upvotes: 5

Related Questions