Skip to content Skip to sidebar Skip to footer

Calling A Php Function Using Ajax/javascript

Ok guys I know this question has been asked before but I am very new to PHP and JavaScript and hadn't even heard of ajax until i started looking for an answer to this question so d

Solution 1:

It does nothing becuase you don't do anything with the result. My guess is that in the example you took, it does some work and doesn't show anything to the user. So if you just had some stuff you wanted to run on the server without returning any output to the user, you could simply do that, and it would work.

Example from jQuery's .get() documentation

What you do:

Example: Request the test.php page, but ignore the return results.

$.get("test.php");

What you want to do:

Example: Alert out the results from requesting test.php (HTML or XML, depending on what was returned).

$.get("test.php", function(data){
    alert("Data Loaded: " + data);
});

Solution 2:

Take a look at the .get() documentation. You're using it incorrectly.

You should be passing data (optional) and handling the data that gets returned, at a minimum:

$.get("backend.php",
    {
        // data passed to backend.php goes here in 
        //
        // name: value
        //
        // format. OR you can leave it blank.
    }, function(data) {
        // data is the return value of backend.php
        // process data here
    }
);

If you pass data, you can retrieve it on backend.php using $_GET. In this case:

$_GET['name'];

Solution 3:

$.get("test.php", { name: "John", time: "2pm" }, function(data) {
   alert("Data Loaded: " + data);
 });

http://api.jquery.com/jQuery.get/


Solution 4:

This would alert the data. right now that function only returns false.

$.get('backend.php', function(data) {
  alert(data);
});

Solution 5:

Your code will not print to the page the way you have it set up; you're part of the way there, in that you have called the page, but the response needs to be handled somehow. If you open up the developer tools in Chrome, you can click on the Network tab and see the request and response to verify that what you coded is actually working, but now you need to put the response somewhere.

By passing a function as the second variable into $.get, you can make your request show up on the page. Try something like this:

$.get("backend.php", function (data) { $('body').append(data); } );

Post a Comment for "Calling A Php Function Using Ajax/javascript"