2

Ok guys I know this question has been asked before but I am very new to PHP and JavaScript and hadn't even heard of ajax until i started looking for an answer to this question so do not understand previous answers.

I am creating a site that essentially is a bunch of videos in a SQL database, it shows one video at a time, I would like to have a next and previous video buttons.

However I cant get past this ajax thing so my question is even simpler. I have looked at this question/answer and think it pretty much sums up what im asking:

How do I run PHP code when a user clicks on a link?

I have copied that exact code,

    <script type="text/javascript">
       function doSomething() {
       $.get("backend.php");
          return false;
      }
     </script>

<a href="#" onclick="doSomething();">Click Me!</a>

And in my backend.php file i have literally just got <?php echo "Hello" ?> just to test it and therefore my understanding is that when i click the link the javascript onClick event is trigged which in turn calls the backend.php file, which says to print "Hello" to the page. However when i click the link it does nothing.

Eventually obviously im going to need to get a lot more complex with my php functions and calling variables and all that stuff but i like to figure things out for myself for the most part so i learn. However im stuck on this bit. Also whilst im here i will ask another thing, I want to 'give back' to the users of the site for answering my questions but I can only really well enough in HTML and CSS to answer other peoples questions, any advice on being able to find the simpler questions on here so i can answer some.

Thanks in advance :)

Community
  • 1
  • 1
DavidT
  • 2,341
  • 22
  • 30

6 Answers6

3

It does nothing becuase you don't do anything with the result. My guess is that in the example you took, it does some work and doesn't show anything to the user. So if you just had some stuff you wanted to run on the server without returning any output to the user, you could simply do that, and it would work.

Example from jQuery's .get() documentation

What you do:

Example: Request the test.php page, but ignore the return results.

$.get("test.php");

What you want to do:

Example: Alert out the results from requesting test.php (HTML or XML, depending on what was returned).

$.get("test.php", function(data){
    alert("Data Loaded: " + data);
});
sachleen
  • 30,730
  • 8
  • 78
  • 73
  • Thanks, I didnt realise that what i was actually using was from the JQuery library, I shall go to the documentation and read up on the `.get()` function and see if i can figure it out. – DavidT Aug 23 '12 at 18:07
3

Take a look at the .get() documentation. You're using it incorrectly.

You should be passing data (optional) and handling the data that gets returned, at a minimum:

$.get("backend.php",
    {
        // data passed to backend.php goes here in 
        //
        // name: value
        //
        // format. OR you can leave it blank.
    }, function(data) {
        // data is the return value of backend.php
        // process data here
    }
);

If you pass data, you can retrieve it on backend.php using $_GET. In this case:

$_GET['name'];
Matt
  • 6,993
  • 4
  • 29
  • 50
1
$.get("test.php", { name: "John", time: "2pm" }, function(data) {
   alert("Data Loaded: " + data);
 });

http://api.jquery.com/jQuery.get/

Oliver Tappin
  • 2,511
  • 1
  • 24
  • 43
1

This would alert the data. right now that function only returns false.

$.get('backend.php', function(data) {
  alert(data);
});
Hanlet Escaño
  • 17,114
  • 8
  • 52
  • 75
1

Your code will not print to the page the way you have it set up; you're part of the way there, in that you have called the page, but the response needs to be handled somehow. If you open up the developer tools in Chrome, you can click on the Network tab and see the request and response to verify that what you coded is actually working, but now you need to put the response somewhere.

By passing a function as the second variable into $.get, you can make your request show up on the page. Try something like this:

$.get("backend.php", function (data) { $('body').append(data); } );
Bryan Agee
  • 4,924
  • 3
  • 26
  • 42
0

Your code is not handling with that data. So instead, you should use following code :

$.get("backend.php", function(response) {
    alert(response);
  })

Or, to show that data on UI, assign it to any html element.

For more understanding , please visit :jQuery.get() link

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101