Omar Abid
Omar Abid

Reputation: 15976

Problem with AJAX and PHP

I have a small problem, I want to load data from a PHP file and put them on a DIV.

Here's the Jquery code

    // Store the username in a variable
    var jq_username = $("#txt_checkuser").val();
    // Prepare the link variable
    var link = 'user.php?action=check&username=' + jq_username;
$('div #checkuser_hint').load(link);

So it works! but instead of loading the result (compiled PHP) it loads the PHP code.

If I write the long URL "http://localhost/project..." it doesn't load anything!

Any idea how to do that?

Upvotes: -1

Views: 75

Answers (3)

OIS
OIS

Reputation: 10033

Does the page return anything when you use your browser?

Are you sure it should not be 'div#checkuser_hint' instead of 'div #checkuser_hint' ?

And this looks like the correct way according to the documentation.

var link = 'user.php';
$('div#checkuser_hint').load(link, {'action':'check', 'username':jq_username});

Upvotes: 1

Sampson
Sampson

Reputation: 268334

Are you able to access the script manually on your own? (try accessing it via your browser: htp://localhost/...) It may be the case that you're missing your opening <?php and/or closing ?> in the script-file itself.

Upvotes: 0

NSSec
NSSec

Reputation: 4551

I think you might be accessing your javascript file as a file on your local filesystem, a request to the same directory would go through the filesystem and not through your webserver, processing the PHP into the desired output. This also explains why http://localhost/project for the AJAX call doesn't work: Javascript might be enforcing the same-origin policy on you.

Verify that you're actually accessing this javascript file through http://localhost/ (as opposed to something like file://C:/My PHP Files/ ).

Upvotes: 4

Related Questions