简体   繁体   中英

Passing PHP variable on calling Javascript function on another page

I am writing a piece of code with 3 files. Ill explain here for simplicity. Files are a,b,c. I have "File a" which is a HTML file "File b" is a htm file "file c" is a php file.

"File a" has "File b" embedded in it by using the HTML include functionality.

The contents of "File b" are designed in "File c".

The PHP+HTML code in file C has a onclick function which calls a JavaScript method on the "file a(HTML)" page. The JavaScript function is written on the "File a". However, I am trying to pass a PHP variable in "File c".

code is as follows:

File a: JavaScript function:

function toggle(id,link) {
    alert("here it is!"+link);
    alert("-----"+id);
    var e1 =  id;
    var e = document.getElementById(e1);
    if(e!= null){
        alert("here it is!2");
    if (e.style.display == ''){
        alert("here it is!3");
    e.style.display = 'none';
    link.innerHTML = 'Show Details';
    }else{
        alert("here it is!4");
        e.style.display = '';
        link.innerHTML = 'Hide Details';
        }
    }
}

....

    <div class="lh">  
              <h2>Next Event</h2>
        <!--#include virtual="fileB.htm" -->
             </div>
    </div>

File c:

$dataNew .= "

          <p>
            <h4><i>". $row['description']."</i></h4>
          </p>
          </div>
              <a href=\"javascript:toggle();\" onclick = \****"toggle('".$idDiv."',this)\****">Show Details</a><br><br><img src=\"/ec/sitepics/soldout.jpg\" width=\"150\" height=\"25\" alt=\"Buy Tickets\" /><BR>
            </div>\n";

The PHP variable passed in File c on the BOLD line, is not retrieved in the JavaScript function.

What kind of change do I need to do or what would I need to add?

I tend to agree with Dany. I'm guessing you primarily work in HTML and Javascript and starting to lever in PHP. I would rename my file a as .php (assuming that's required to associate it with php) then use the PHP include to include file c:

<?php
include("<file c>");
?>

instead of your html incude method.

Remember (because it is server-side) all PHP script is executed - then the resulting output script is delivered to the browser for HTML and javascript processing. This can be hard to get your head round at first - it was for me.:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM