可能重複:
how to put javascript variable in php echo爲什麼document.write + echo不起作用?
我有PHP的問題。我想在JavaScript中使用PHP變量。但是,它不起作用! $doccontent
只顯示一次!這裏是Test.php
(頁面指定參數,如Test.html
):
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<?php
$page = $_GET["page"];
$doc = new DOMDocument();
$doc->loadHTMLFile($page);
$doc->formatOutput = true;
$contentnode = $doc->getElementById('content');
$doccontent = new DOMDocument();
$doccontent->appendChild($doccontent->importNode($contentnode, true));
$doccontent = $doccontent->saveHTML();
?>
</head>
<body>
<?php
echo $doccontent;
?>
<script type="text/javascript">
document.write("<?php echo $doccontent;?>");
</script>
</body>
</html>
這裏是Test.html
:
<!DOCTYPE html>
<html>
<head>
<title>Hey</title>
<link rel="stylesheet" type="text/css" href="Edit.css">
</head>
<body>
<button id="admin"><a href="Edit.php?page=Edit.html">Edit</a></button>
<h1>Automatica</h1>
<div id="content">
<h2>Header 1.1</h2>
<p>Test</p>
<h3>Header 1.1.1</h3>
<p>Test</p>
<h3>Header 1.1.2</h3>
<p>Test</p>
<p>Test</p>
<h4>Header 2.0.0.1</h4>
<p>Test</p>
</div>
</body>
</html>
你確定'$ doccontent;'有一個值嗎?如果是這樣,它是文字?它是否包含「(雙引號)?如果是這樣,它可能會破壞你的'document.write' –
你能告訴我們爲什麼你想要通過這些箍來將HTML頁面加載到當前頁面嗎? – mplungjan
@hakre :實際上是相反的 – Agantacroxi