Calling a php function using ajax/javascript

前端 未结 6 1283
庸人自扰
庸人自扰 2021-01-21 16:21

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

相关标签:
6条回答
  • 2021-01-21 16:59

    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'];
    
    0 讨论(0)
  • 2021-01-21 17:00

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

    $.get('backend.php', function(data) {
      alert(data);
    });
    
    0 讨论(0)
  • 2021-01-21 17:03

    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);
    });
    
    0 讨论(0)
  • 2021-01-21 17:08
    $.get("test.php", { name: "John", time: "2pm" }, function(data) {
       alert("Data Loaded: " + data);
     });
    

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

    0 讨论(0)
  • 2021-01-21 17:09

    Your code is not handling with that data. So instead, you should use following code :

    $.get("backend.php", function(response) {
        alert(response);
      })
    

    Or, to show that data on UI, assign it to any html element.

    For more understanding , please visit :jQuery.get() link

    0 讨论(0)
  • 2021-01-21 17:19

    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); } );
    
    0 讨论(0)
提交回复
热议问题