0

Hello I have this simple problem in my code. I'm using php echo to use my javaScript inside in my model, my question is I want to use my JS variable inside php echo and pass it through url using GET method my problem is my variable is read as string a variable

echo  "<script>
         $('.btnclick').on('click', function(){
             var ris_id = $(this).attr('data-id');

             window.open('pdf_report?d="."ris_id"."' , '_blank');
         });
       </script>"

Here's my sample code, my problem is that I want to pass the ris_id as a variable not a string to the new tab that will open after click the button.

PPDY
  • 1
  • 1
  • Avoid using `echo` if your output has no PHP in it. Just close the PHP context with `?>` and write your plain HTML / JS as normal – Phil Jul 21 '22 at 04:20
  • Alternately, simply use a template literal which means nothing to PHP... `window.open(\`pdf_report?d=${ris_id}\`, '_blank')` – Phil Jul 21 '22 at 04:24
  • I can't avoid using the echo because the button is generated from the model and the " – PPDY Jul 21 '22 at 05:07
  • There is absolutely no difference between `echo "";` and `?> – Phil Jul 21 '22 at 05:11
  • using inside a php function gives me error – PPDY Jul 21 '22 at 05:31
  • Seems fine here ~ https://3v4l.org/JB6tk – Phil Jul 21 '22 at 05:48
  • I just solved it :D I transfer my js script to a external file so that I will not use the echo anymore. thanks for the response guys. – PPDY Jul 21 '22 at 06:03
  • None of that output you are creating there is "dynamic", it does not depend on any PHP values or conditions - so it should have been written without echo to begin with. https://www.php.net/manual/en/language.basic-syntax.phpmode.php – CBroe Jul 21 '22 at 07:08

2 Answers2

-1

I think you should use simple javascript not jquery.

-1

The concatenation of string in JS or jquery will be done by (+) not by (.)

window.open('pdf_report?d="'+ris_id+'"' , '_blank');
Vinod Patidar
  • 381
  • 4
  • 11