0

I am trying to load the output of a PHP script into a using JavaScript and JQuery. The JavaScript function I am using uses the $.get function in JQuery to call a php script, which I want to display in another division. The script I wrote is:

<script type="text/javascript">
function on_load() {
    $(document).ready(function() {
        alert('here');
        $.get("http://localhost/dbtest.php", function(data){
            alert('here too too');
            $("uname").html(data);
        });
    });
}

</script>

The PHP script (dbtest.php) uses a simple echo statement:

echo "hello, world!!!";

I am getting the first alert here, but not the second. What Can I be doing wrong here?

8
  • 7
    Check your console, what does it say? Commented Sep 3, 2013 at 19:20
  • 4
    what is $("uname")? this should be valid selector Commented Sep 3, 2013 at 19:21
  • 1
    How do you test that page? Which url do you use? If you are testing that html with file protocol(by doubleclicking it for example), you can not make AJAX request to localhost. That HTML code will be served by localhost too. Commented Sep 3, 2013 at 19:25
  • Have you tried going straight to localhost/dbtest.php? Does that page load? The second alert will only show on a succesful return from "localhost/dbtest.php" Commented Sep 3, 2013 at 19:28
  • I have tried opening the dbtest.php file in the browser, and it works. I am trying to write the HTML page for an android application using Phonegap Commented Sep 3, 2013 at 19:29

6 Answers 6

2

I suppose uname is a ID, in that case you should use:

$("#uname").html(data);

You can add this to your php for debugging:

error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", 1); 

Try also to remove http:// from your ajax call and use relative path instead.

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

7 Comments

I have set the Error Reporting flags as you mentioned, and also made the selector correction. Not getting the right output either with the http:// prefix or without it
@saurabhsood91, what do you get in the console?
@saurabhsood91 if you have a live link could help.
Not getting any errors in console. I am actually running the javascript code in an HTML file, which I am running along with Phonegap. I tried the same script along with the browser, and it is not working.
In fact, adding the prefix got the code working in the browser, on the server, but not inside an android application running phonegap
|
0

The guys below pointed out that the selector is wrong. Indeed that's a problem, but I think that the real issue is that you don't get the second alert. Probably your php file localhost/dbtest.php is not accessible. What happen if you open localhost/dbtest.php in a new tab?

2 Comments

I am able to open the file properly. the selector was indeed a mistake, but making that right didnt work
It should be something with the url. What you see in the Chrome DevTools, Network panel. Do you see your request there? What is the response?
0

I think the problem is the path to your dbtest.php file. You are saying you seecond alert will not be displayed so your request must be wrong.

Try to copy your page into the same folder like dbtest.php open the page in your browser with http://localhost/yourfile.php

If this does not display both alert-boxes try the same with an open developer console (Chrome/IE = F12) and look if there are errors.

Comments

0

You say you are trying to make ajax requests to your localhost from a Phonegap application. Phonegap prevents making ajax requests to other domains by default. You must add localhost to whitelist. Here is more detail: http://docs.phonegap.com/en/1.9.0/guide_whitelist_index.md.html

Comments

0

Here you have a working example:

<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>

<div id="uname"></div>

<script>
function on_load() {
    $(document).ready(function() {
        alert('here');
        $.get("http://localhost/dbtest.php", function(data){
            alert('here too too');
            $("#uname").html(data);
        });
    });
}

on_load();
</script>

Beware of the same-origin-policy. The page loading dbtest.php must be from the same origin, unless you grant other origins by adding a header from dbtest.php.

1 Comment

Because I tested it before I posted..?
0

Try add some error handling to your code to better see what´s happening.

<script type="text/javascript">
function on_load() {
    $(document).ready(function() {
        alert('here');
        $.get("http://localhost/dbtest.php", function(data){
            alert('here too too');
            $("uname").html(data);
        }).fail(function () {
            alert("failed");
        });
    });
}
</script>

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.