如何将php include调用作为javascript函数的变量传递?

时间:2016-02-02 23:25:10

标签: javascript php

我有一个页面使用javascript innerhtml将元素的内容替换为伪静态页面。

以下代码来自该元素的当前内容:

<?php
$con = mysql_connect('***********', '******', '**********');
mysql_select_db(dbbmdmi);

if ($con) {
    $sql = "SELECT TeamID, Captain, CountryID, ArrDate, DepDate FROM Teams";
    $result = mysql_query($sql) or die(mysql_error());

    $link = "<a id='" . $row["TeamID"] . "' onClick='SetTeamID();ReplaceContentInContainer('content','replace_target6')'>Edit</a>";

    if ($result) {
        // output data of each row
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            echo "Captain: " . $row["Captain"]. "<a id='" . $row["TeamID"] . "' onClick=\"ReplaceContentInContainer2('content','<?php include 'scripts/editteam.php?TeamID=" . $row["TeamID"] . "'>')\">Edit</a>;" . "</br>Country: " . $row["CountryID"]. "</br>Dates: " . $row["ArrDate"]. " - ". $row["DepDate"]. "<br>";
        }
?>

onClick=\"ReplaceContentInContainer2('content','<?php include 'scripts/editteam.php?TeamID=" . $row["TeamID"] . "'>')中我试图将php include作为“source”参数传递给原始“容器”页面中的以下函数:

<script type="text/javascript">
    function ReplaceContentInContainer2(target,source) {
        document.getElementById(target).innerHTML = source;
    }
</script>

点击链接后,我一直收到此错误:

  

teams.php:1SyntaxError:意外的标识符'脚本'。预期')'结束参数列表。

基本上,我正在尝试传递$row_["TeamID"] to the next page in the address so that I can call with a $ _ GET`函数。

有什么建议吗?

更新

在javascript函数调用中转义单引号后,元素的内容将被替换为应有的内容,除了php include在新内容中呈现为html注释,而不是以php运行。

onClick=\"ReplaceContentInContainer2('content','<?php include \'scripts/editteam.php?TeamID=" . $row["TeamID"] . "\'?>')\"

中的单引号转义

渲染html:<!--?php include 'scripts/editteam.php?TeamID=Kellog'?-->

1 个答案:

答案 0 :(得分:0)

这是一个非常有趣的想法。您将遇到的问题是PHP只是呈现您的<?php include... ?>字符串,因为PHP最初只处理它,然后将其交给它。这可能是你想要做的。

<!-- front-end.html-->
<script type="text/javascript">
function ReplaceContentInContainer2(target,source) {
    document.getElementById(target).innerHTML = source;
}
function ajaxForHtml(url, callback) {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      callback(xhttp.responseText); 
    }
  };
  xhttp.open("GET", "ajax_info.txt", true);
  xhttp.send();
}
function clickAction(id) {
  ajaxForHtml('scripts/editteam.php?TeamID=' + id, function(data) {
    ReplaceContentInContainer2('content', data);
  });
}
</script>
<?php
$con = mysql_connect('***********', '******', '**********');
mysql_select_db(dbbmdmi);

if ($con) {
    $sql = "SELECT TeamID, Captain, CountryID, ArrDate, DepDate FROM Teams";
    $result = mysql_query($sql) or die(mysql_error());

    $link = "<a id='" . $row["TeamID"] . "' onClick='SetTeamID();ReplaceContentInContainer('content','replace_target6')'>Edit</a>";

    if ($result) {
        // output data of each row
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
          echo sprintf('Captain: %s <a id="%d" onclick="clickAction(%d);">Edit</a>;</br>Country: %d</br>Dates: %s - %s </br>',
                        $row['Captain'], $row['TeamId'], $row['TeamId'], $row["CountryID"], $row["ArrDate"], $row["DepDate"]
               );
         }