Search code examples
javascriptphpjquerysendhttp-get

Jquery send data to PHP and receive data


I have a PHP script calling an API which takes a while to load, consequently users do not see the page for a while and believe something went wrong.

I have been informed that a Jquery script can send data to the PHP file, get the resulting data from it and display a loading message/animation while this is being performed. Looking around the Internet I found this:

$.get("check.php", { url: "www.domain.com"}, function(data){
    alert("Data Loaded: " + data);
});

I've not been able to figure out how the PHP file needs to receive the sent data ($_GET['url'] or otherwise) or how to receive and display the data and how to display a loading message. A lot of questions I know, but I would be very grateful for any information to understand how to do this.


Solution

  • With the help of everyone who responded to this post this is the working result:

    <div id="content-area"><p>loading content...</p></div>
    
    <script type="text/jscript">    
        $.get("check.php", { url: "www.domain.com"}, function(data){
        $("#content-area").html(data)
        });
    </script>
    

    The PHP receives the data with a GET method and returns data with echo.

    Thanks everyone for your help :)