1

I'm creating a web-based application that interfaces with a MySQL database on the same server. I'm using Javascript/jQuery to form the query, and then trying to use php to query the database.

Currently, I'm just trying to learn AJAX requests and php (testing on my local computer). Particularly, I'm trying to "alert" the result of my php code and add that to a div. But instead, I get an alert with the raw php code and my div is just blank. (This isn't the case if the url was set to a normal text file instead).

How can I make it such that the callback's variable called "result" is set to the output of the php script?

Below is my jQuery code for the ajax request:

$.ajax({

  url: 'getData.php',
  data: queryString,
  method: 'GET',
  cache: false,
  success: function(result) {
    alert(result);
    $('#dataTable').html(result);
  },
  error: function(jqXHR, textStatus, errorThrown) {
    if(jqXHR.status == '500') {
      alert('Internal server error: 500');
    } else {
      alert('Unexpected error');
    }
  }
})

And here's my simple php file:

<?php
echo "huh";

?>
10
  • 1
    Are you doing this from a web server? Commented Jul 15, 2015 at 20:48
  • PHP not installed? PHP not enabled? PHP not enabled on userdir(for apache)? Php5-fpm not installed(for nginx)? Misconfiguration? Commented Jul 15, 2015 at 20:52
  • Is your 'simple php' file being parsed as PHP? Does it have a .php extension? Commented Jul 15, 2015 at 20:55
  • Should you set a dataType? Commented Jul 15, 2015 at 20:55
  • 1
    Well PHP is not a client side language. It is a server side language, so it needs webserver. You can write javascript and html and it will work with your browser because they are client side, but not PHP. Commented Jul 15, 2015 at 21:30

1 Answer 1

1

If I understand your post and comment below, you open your page from local path. ex. file:///C:....

In the AJAX request you make request to file without specifying host name. ( getData.php )

In this case your request isn't handled by any webserver and will not be interpreted by PHP processor.

To execute PHP code, you must request to some webserver (with installed PHP). ex http://localhost/getData.php

But if you just want to learn AJAX you can simply request for some html file.

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

Comments

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.