0

Okay so I have a basic index html file with a button that functions like this:

$("#button").click(function() {
    $.get("./echo.php", function(data) {
        alert(data);
    });
});

I have one page called normal.php and all it has is some text:

Hello Test

I have another page called echo.php and for this basic example, it can find "Hello Test" in the normal.php file using regex:

<?php
    $contents = file_get_contents("./normal.php");
    preg_match('/Hello Test/s', $contents, $matches);
    echo $matches[0];
?>

Makes sense so far? Okay this works all nice and well. You press the button, and it alerts "Hello Test" on the index page just fine. Likewise, if you just open up the echo.php page, it will just display "Hello Test" on the page.

Now the issue comes up when I am trying to access another website. So I'm basically trying to scrape weather data for a basic weather scraping website, and let's say echo.php is updated to have this following code instead:

<?php
    $contents = file_get_contents("http://www.weather-forecast.com/locations/london/forecasts/latest");
    preg_match('/3 days/s', $contents, $matches);
    echo $matches[0];
?>

If you just load up echo.php manually on a web browser, this works. It will display "3 days" like it's supposed to. However, when I click the button on the website, nothing happens. It doesn't alert "3 days" like it's supposed to, even though it does display on the page just fine if echo.php is loaded up. What is the issue here?

I have been at this for a few hours already. I'm using the latest PHP version (7.2) and the latest JQuery (3.3.1). I've taken a look at these other threads but they were all similar but they did not solve my problem:

JQuery GET request won't get correct data from php echo

Problem with the jquery $.get function

jQuery get Function Not Returning Data

jQuery get request not retrieving HTML

Problem with the jquery $.get function

3
  • Did you try to check what is going on when you click the button using your browser dev tools? Commented Apr 26, 2018 at 22:51
  • @Lou I don't think you can view PHP events that way, with PHP it can show an error directly on the page or output it to an error log, in either case there's no error showing up anywhere. Also testing out the dev tools to see what's going on in JavaScript, it also doesn't find any errors. Commented Apr 27, 2018 at 0:00
  • You can validate that the ajax call is actually being launched and the output of the call if any. Commented Apr 27, 2018 at 0:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.