0

I was wondering if there's a way I can use the jQuery load with php code? Basically I'm trying to create a inbox and this is for where you can read your PMs, and I want them to load inside the div #main-news.

I tried this:

$('#main-news').load('inc/readpm.php?id=<?php echo $pmid; ?>');

But it doesn't work and since php is serversided and jQuery is clientsided I guess there is no easy way of doing this?

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Donran
  • 15
  • 1
  • 4
  • 1
    this should work.. jquery load should be requesting the correct route. check your firebug console for the exact error – Mithun Satheesh Nov 23 '13 at 14:19
  • This will work just as you load a HTML page. Of course, the PHP file has to be outputting some HTML. – Pekka Nov 23 '13 at 14:23

2 Answers2

0

If you have this code:

$('#main-news').load('inc/readpm.php?id=');

inside a PHP file, there should not be any problem. But of course, if this code is located in a .js file, then it won't be interpreted by PHP.

deanpodgornik
  • 322
  • 2
  • 7
0

Options:

  1. AJAX to load the messages on-demand - http://api.jquery.com/jQuery.ajax/ (I can clarify more if you are unfamiliar, just lemme know).

  2. Pass the PHP data through variables to Javascript directly like you're already doing. If it's an external Javascript file you want to transfer the variables to, declare and assign the variables before the file is loaded in your PHP document like this:

    <script>

    pmBox = "<?php echo $pmid; ?>";

    </script>

    <script src="externalFile.js" type="text/javascript">

    </script>

Hope this helps. Apparently Stack Overflow doesn't like me posting <script> in the normal code formatted 4-space CTRL+K way.

Oh well.

Deryck
  • 7,608
  • 2
  • 24
  • 43