第一页.html :
<body>
<?php
$text = $_POST['text'];
?>
<p style = "color:red; " id = "getext"><?php echo $text; ?></p>
</body>
第二页.php :
<body>
<?php
$text = $_POST['text'];
?>
<p style = "color:red; " id = "getext">i want $text var to appear here</p>
</body>
我想在 ID 为“gettext”的 p 元素中将文本节点从第一页发布到第二页,在此先感谢
最佳答案
我们不能直接发布文本节点。所以我们必须为此改变逻辑。
<form action = "secondpage.php" method = "POST" onsubmit="return false" name="form1">
<p id="txtNode">textnode</p>
<input type=button onclick="submitForm()" value="Submit">
<input type=hidden id="text" name="text">
</form>
这里我们通过设置onsubmit="return false"禁用了表单的默认提交 Action ,然后我们为按钮定义了onclick="submitForm()"将提交表格。在submitForm() 函数中我们将文本节点复制到一个隐藏字段中并提交表单,如下所示:
function submitForm(){
var txtNode = document.getElementById("txtNode").innerHTML;
document.getElementById("text").value=txtNode;
document.forms["form1"].submit();
}
然后在 secondpage.php 上,您将能够获取该值:
<?php
$text = isset($_POST['text'])?$_POST['text']:"";
?>
<p style = "color:red; " id = "getext"><?php echo $text; ?></p>
关于javascript - 如何使用 PHP 将 Textnode 从一个页面发布到另一个具有特定 ID 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43831290/