0

In my $.ajax() from my JQuery script written inline in my blade.php view file, I have received a path to an image file (relative to public folder, like /thumbnails/87991.png) as a response. Say the response in success() of $.ajax() is stored in variable dataReturned.

console.log('dataReturned:'+dataReturned);
// Prints the correct '/thumbnails/87991.png'

So in the success() of $.ajax(), I want to set that path as the value of a src attribute for an <img> on that page. For that, I want to generate a valid URL by passing it to the Laravel's url() helper. Like as follows.

var thumbnailPhotoPathOnServer = "{{url(dataReturned)}}"; 
var thumbnailPhotoPathOnServer = "{{url('dataReturned')}}"; // I know it makes it a string but i still tried it out of desperation =(
var thumbnailPhotoPathOnServer = "@php echo url(dataReturned); @endphp";

I have tried all these statements but I keep getting

Use of undefined constant dataReturned - assumed 'dataReturned' (this will throw an Error in a future version of PHP)

So that question is that what am I missing here and what should I do about it?

NXA
  • 440
  • 2
  • 10
  • 1
    PHP can't read any JavaScript variables. PHP is executed first, on the server, and sends the output to the client - then the client runs the JavaScript, which is client-side. – Qirel Jul 04 '20 at 14:28
  • You can send a request to the server through AJAX, where you send an argument which is the path, then return the `url($request->dataReturned);` - if you really need it (but I fail to see why when all you're doing is passing it through `url()` in PHP). – Qirel Jul 04 '20 at 14:31
  • @Qirel Thank you, my bad! I am sorry. Now, this modifies my question. I need to use that dynamically generated path to generate URL, in order to display my picture in my ``. There must be a way to do that? – NXA Jul 04 '20 at 14:32
  • If the JS variable `dataReturned` is generated clientside, can't you just display that directly? You don't need the full URL, you can do ``. – Qirel Jul 04 '20 at 14:33

0 Answers0