M
M
Mickey-D2021-07-14 14:28:40
PHP
Mickey-D, 2021-07-14 14:28:40

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

1 answer(s)
A
Alexander, 2021-07-22
@Azurre

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>";

PS
What you describe looks like a very controversial decision and it would be better for you to use it only as a practice and experience.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question