0

This is my JavaScript code:

$(FORM).on('submit', function (event) {
    event.preventDefault();
    $.get('server/example.php?number1=34&number2=46', function (response) {
        console.log(response);
    });
});

And this is my PHP file called example.php located in a directory called server:

<?php
  $num1 = $_GET['number1'];
  $num2 = $_GET['number2'];
  $sum = $num1 + $num2;
  echo($sum);
?>

When I make my get request, the response in Chrome DevTools (and console) prints the PHP code as the response instead of the value 80 (the sum of adding 34 and 46) when the console.log(response) is called. I've tried the $.ajax() method as well as explicitly instantiating and using the XMLHttpRequest object. They all return/print the PHP code instead of 80. How do I get the value of $sum?

I use BrowserSync when developing if that makes a difference (if it does, how can I do it differently).

10
  • 2
    sounds like PHP is not installed or activated properly on your webserver, and/or you're running over file:// instead of http://. BTW What happens if you visit the same URL directly in your browser's address bar? And what is the relevance of browsersync? Commented Aug 2, 2018 at 15:03
  • 2
    Sounds like you don't have a server set up properly, and PHP is being treated as a static file. Commented Aug 2, 2018 at 15:05
  • 1
    Great, see the first part of my first comment. Commented Aug 2, 2018 at 15:06
  • 1
    If you are not sure how to set up a local web server then have a look at WAMP Server. There is also XAMMP, MAMP, LAMP depending on your operating system. Commented Aug 2, 2018 at 15:28
  • 1
    Check out stackoverflow.com/q/5121495/2191572 for some detailed info Commented Aug 2, 2018 at 15:29

1 Answer 1

1

According to https://scotch.io/tutorials/how-to-use-browsersync-for-faster-development

"BrowserSync creates a small server"

...so I guess it can only serve static HTML pages, not PHP. You need to set up a proper webserver like Apache and install PHP into it. As mentioned in the comments, it can all be downloaded and installed separately, or there are packages such as LAMP, XAMPP and others which will give you a whole Apache, PHP and MySQL development stack installed easily.

The page also says

if you already have a server setup, BrowserSync can hook into that server and act as a proxy.

which means that once you've got your proper server set up, you can still take advantage of BrowserSync's features at the same time, by connecting it to your server

Sign up to request clarification or add additional context in comments.

2 Comments

Aah, thanks! Now, my focus isn't really to learn PHP and such, but only client-side stuff (currently attending a university course). Do you have any tips for testing out Ajax calls without the need for a web server? Like for example trying my code above?
There are a few sites around which allow you to mock an endpoint and just return the same static data every time. So it's a bit less flexible if you're trying to test the effect of sending different parameters and values, but it does at least let you do some basic testing. myjson.com is just one such site, for example. You can paste in some JSON and save it, and it'll give you a URL which you can use in your AJAX call, which will always return that data.

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.