0

I'm using jquery ajax can i use php code in success: function(html){} ??

phihag
  • 278,196
  • 72
  • 453
  • 469
Shaun
  • 2,313
  • 7
  • 36
  • 43

5 Answers5

2

No, jquery/JavaScript code is executed at the client side. You can either write JavaScript/jQuery code or preprocess the answer to the XHR request in php.

phihag
  • 278,196
  • 72
  • 453
  • 469
0

No, not directly, as JavaScript ist executed on the client side, and PHP is executed on the server.

A workaround would be to send another AJAX-Request from your success-function. But it can only do things on the server side, not on the client side.

If you just want to download a file from your callback function, use location.href = url. The URL can be a server side PHP script serving the data stream, or a real file.

Danilo Bargen
  • 18,626
  • 15
  • 91
  • 127
  • ajax could have a callback that does stuff on the client side after something is returned. – dogmatic69 Mar 04 '11 at 12:03
  • Yes but that's another clientside callback-function, in that case he would probably be able to do this directly in the first one, instead of nesting two ajax requests. – Danilo Bargen Mar 04 '11 at 12:05
  • Ok i m ready to use nested ajax calls, will it allow me to download a PDF ?? – Shaun Mar 04 '11 at 12:15
  • @user418232: Are you getting the PDF from a data stream or from a real file? If it's a real file, you can get it directly with JavaScript. If it's an data stream, provide a server side script that dumps your data stream with the corresponding headers and then access it from JavaScript as if it were a real file. See my answer edit. – Danilo Bargen Mar 04 '11 at 12:24
  • yes i did that `location.href = url` but it opens the file in a new tab in the browser i want a dialogue to save the PDF – Shaun Mar 04 '11 at 12:45
  • @user418232: you need `header('Content-Disposition: attachment; filename="downloaded.pdf"');`, then the script will enforce the download – Danilo Bargen Mar 04 '11 at 13:05
  • It did 'nt enforce the downlaod but when i checked it through firebug the response is something like `%PDF-1.7 6 0 obj << /Type /Page /Parent 1 0 R /LastModified (D:20110304181659+05'00') /Resources 2 0 R /MediaBox [0.00 0.00 595.28 841.89] /CropBox [0.00 0.00 595.28 841.89] /BleedBox [0.00 0.00 595.28 841.89] /TrimBox [0.00 0.00 595.28 841.89] /ArtBox [0.00 0.00 595.28 841.89] /Contents 7 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Annots [ 5 0 R ] /PZ 1 >> endobj7 0 obj<> stream x��SMo�0��W�c{�IB��C��rk{�E�VѢIh��6gKŝ........` – Shaun Mar 04 '11 at 13:19
  • @user418232: Well then there's a problem with your PDF data, or you are dumping it in a wrong way. Please post a new question. – Danilo Bargen Mar 04 '11 at 13:21
  • @user418232: don't request the document via ajax, that makes no sense. call it via location.href directly, and set the appropriate headers in the file serving it. – Danilo Bargen Mar 04 '11 at 13:52
  • This is because my PDF takes 1.5 minutes to load via normal PHP code.So i thought i can call ajax function and show the user loader before response from the server....... – Shaun Mar 04 '11 at 14:17
  • Then ask an according separate question. I don't know the answer. – Danilo Bargen Mar 04 '11 at 14:24
0

Yes.
For instance, you could do:

success: function(html) {
  <?php echo 'alert("Hello, World!");' ?>
}
seriousdev
  • 7,519
  • 8
  • 45
  • 52
  • 1
    Technically correct answer, but not helpful, as the PHP won't be executed by the success-function. But on the other side, the asker did not specify this requirement in his question. – Danilo Bargen Mar 04 '11 at 12:03
0

Since JS is all client side and PHP is server side, the two cannot be executed within each other synchronously. However, you can use AJAX to ask PHP to execute code on values, but if you already have an AJAX query to PHP going, why not just do it then.

Bailey Parker
  • 15,599
  • 5
  • 53
  • 91
0

You use PHP indirectly anyhow. There is no need to note in the success function:

$.ajax({
     url: "execute.php",
     success: function(html){}

And the html comes directly from the execute.php script. And this is where you do any PHP stuff like:

<?php   echo "<div>" . php_stuff() . "</div>";   ?>

Think of the PHP script as the first half of the execution, and the jQuery success: callback as the second part of the AJAX call.

mario
  • 144,265
  • 20
  • 237
  • 291
  • @user418232: Your question has been answered before http://stackoverflow.com/questions/5192917/force-download-through-js-or-query - Do not repost, especially not lazy one-liners. – mario Mar 04 '11 at 12:47