Answer the question
In order to leave comments, you need to log in
How to pass variable from PHP to JS?
There are 3 files: index.php, pdf-viewer.php and pdf.js. Index.php lists pdf files, pdf-viewer is a form to output pdf, pdf.js is output pdf to a form. Now the pdf file is output via a direct link that is written in js .
The variable contains the path to the file. There is also a variable with the full name of the file .
I need a link from the list to open a page with a pdf file. Is it possible to somehow pass a variable with a link from php to js.
List of files created by loop:
index.phpconst url = "tmp/file.pdf";
$urlFile
$path_parts['basename']
foreach (glob("tmp/*.pdf") as $urlFile) {
$path_parts = pathinfo($urlFile);
echo "
<li>
<div>
<div>
<a target='_blank' href='pdf-viewer.php'>". $path_parts['basename']."</a>
</div>
</div>
</li>";
Answer the question
In order to leave comments, you need to log in
echo "const fileName = '$fileName';";
The script will have access to such a variable.
You can also pass a variable to a js function as an argument:
echo "<a href='javascript:void(0)' onclick='showPdf($fileName)'>{$path_parts['basename']}</a>";
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question