0

I'm trying to load the contents of a php file into a script using AJAX with the following code:

<button onclick="changeText3()"><img border='0' alt='More Options' src='tmpls/media/images/icons/cog.png'></button>
<script> function changeText3(){
   $("#test").click(function(){
   $("#test").load("file.php");
    }
}
</script>
<div id='test'>test</div>

Nothing happens when I click anything. I'm brand new to AJAX and am likely missing something simple. I've tried a number of different methods I've seen and I've had no luck.

8
  • 1
    Check the console tab in firebug. It will definitely show some error or output. Commented Apr 20, 2017 at 6:44
  • 1
    "nothing happens" - did you check in the developer tools console? Commented Apr 20, 2017 at 6:44
  • 1
    $("#test").click() - Why? Commented Apr 20, 2017 at 6:44
  • 1
    ooh, that would mean you have to click on an empty div (good luck finding it) to actually do the "load" part - well picked up @Webbanditten Commented Apr 20, 2017 at 6:47
  • 1
    You don' need $("#test").click(function(){} event listener, just remove it Commented Apr 20, 2017 at 6:51

3 Answers 3

1
<button id="showOutput">
    <img border='0' alt='More Options' src='tmpls/media/images/icons/cog.png'>
</button>
<script>
    $(document).ready(function(){
        $("#showOutput").click(function(){
            $("#test").load("file.php");
        });
    });
</script>
<div id='test'>test</div>

Here we can see that as you tried JQuery so I removed JS function and catch JQuery event.

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

7 Comments

I get "SyntaxError: missing ) after argument list[Learn More]".
Also, if I change it to $("#test").load("file.php"); });, I get ReferenceError: $ is not defined <anonymous>.
Check now. I missed a bracket.
Still "$ is not defined".
Like I said, I'm brand new to AJAX. I didn't know I had to load JQuery. Everything is working now. Thanks.
|
1

You should try to specify file.php's whole address in your ajax call like this -

$("#test").click(function(){
$("#test").load('url to home.php');
});

If that won't solve the case, you should log a bit more of your ajax response and you'll be able to easily observe and solve it by yourself, like this:

$("#test").load("home.php", function(response, status, xhr) {
   if (status == "error") {
    console.log(msg + xhr.status + " " + xhr.statusText);
   }
});

Check your console for errors.

Comments

1

Corrected your code. You don't need a Event listener, cause you already use onclick="changeText3()"

function changeText3(){
  $("#test").load("test.php", function(response, status, xhr) {
    if (status == "error") {
     console.log(msg + xhr.status + " " + xhr.statusText);
    }
  });
 }

1 Comment

I get "$ is not defined: changeText3, onclick".

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.