-3

It is necessary in PHP code where there is a js code to output, for example, through an alert variable. Does not work. Please help, tell me how to do it. And if anyone can how to use this as a URL to the picture

function setLocation($url_image){
   $locationName = R::load('locationsdb', $url_image);
    echo '<script type="text/javascript">',
        'alert("'<?php echo $locationName; ?>'");',
        'var img = "<?php echo $locationName ?>";',
        'document.getElementById("image_location").src = img;',
        '</script>';
    
}

alert("{"id":"3","location_name":"\u0410\u043b\u0445\u0438\u043c\u0438\u043a\r\n","location_description":"\u0423 \u044d\u0442\u043e\u0433\u043e \u0441\u0442\u0430\u0440\u0446\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0435\u0441\u0442\u0438 \u043d\u0430\u0441\u0442\u043e\u0439\u043a\u0438, \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b\u0435 \u043f\u0440\u0438\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0438\u043b \u0433\u0435\u0440\u043e\u044f\u043c, \u0438\u0441\u0446\u0435\u043b\u044f\u0442\u044c \u0438\u0445 \u043f\u043e\u0441\u043b\u0435 \u043e\u0436\u0435\u0441\u0442\u043e\u0447\u0435\u043d\u043d\u044b\u0445 \u0431\u043e\u0435\u0432 \u0438 \u043c\u043d\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u043e\u043c\u043e\u0447\u044c \u0432 \u0442\u044f\u0436\u043a\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u0438\u0441\u043a\u0430\u0442\u0435\u043b\u044f \u043f\u0440\u0438\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439.\r\n","location_travel":"2","location_lut":"-","location_mobs":"-","location_image":"../images/locationsBackgrounds/backgroundAlchemistImage.png","location_runs":"-","location_characters":"-"}");

  • 3
    You are already in a PHP code section here, so you can not use `` again inside of it. Please go and read up on the basics of the syntax, it should not be our job to explain those here. – CBroe Jul 06 '20 at 11:41
  • if I write this also does not work. Please tell me how to write. Sounds stupid, sorry, but I'm confused echo ''; – IIITUKATUPKA Jul 06 '20 at 11:47

1 Answers1

1

You can't use <?php tags inside each other. It makes no sense anyway - you're already in the PHP block, you don't need to open it again. And for similar reasons you can't use echo inside another echo command. And again it makes no sense anyway, you're already echoing, so why repeat the same instruction before you've finished the first one?

To generate the text you want, just use string concatenation with the . (dot) operator:

echo '<script type="text/javascript">
  alert("'.$locationName.'"); 
  var img = "'.$locationName.'"; 
  document.getElementById("image_location").src = img;
  </script>';
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Gives an error message Uncaught SyntaxError: missing ) in line with alert – IIITUKATUPKA Jul 06 '20 at 13:00
  • 1
    It shouldn't. In theory it produces valid JavaScript - demo: http://sandbox.onlinephpfunctions.com/code/7a8102f17f26aa45fc030a1e43bb9612a0570665 . The only exception would be if for some reason your $locationName contains a quote mark - something like this demo for example: http://sandbox.onlinephpfunctions.com/code/9bf3b998f310e7eaf422dce395b8a3b2378acedd . But I would think maybe it's more likely you didn't copy my code correctly. Double-check it and try again :-) – ADyson Jul 06 '20 at 13:08
  • I'll try to figure it out. Thank's man – IIITUKATUPKA Jul 06 '20 at 13:21
  • Maybe the fact is that the data in the variable is taken from the database? – IIITUKATUPKA Jul 06 '20 at 13:26
  • 1
    That can't directly be the cause, no. It would be a string same as any other, no matter if it was hard-coded or taken from a data source. Take a look at the final generated code in your browser (your browser's View Source or Element inspector features can show you this, or clicking the line number in the JS error will take you to it), and see what JavaScript was actually output on that line. Then you'll be able to see precisely what's causing the syntax error. – ADyson Jul 06 '20 at 13:27
  • I changed the question and at the end I output a line in which it writes an error – IIITUKATUPKA Jul 06 '20 at 13:33