繁体   English   中英

PHP:如何获取当前的innerHTML?

[英]PHP: How to get CURRENT innerHTML?

简而言之,我试图创建一个用户可以在其中创建元素的网站,然后将其放入ID为“ box”的div元素中。 js脚本运行良好,可以创建p元素。

然后,我制作了一个php脚本,在其中保存了“ box” div的innerHTML,然后将其保存在.txt文件中。

现在的问题是,脚本在将p元素添加到其中之前,将innerHTML值返回为原始值。

这是我的PHP脚本:

 <?php
//Basically a function
if(isset($_POST["use_button"]))
{
    //Loads the file, which is named test.php
    $dom= new DOMDocument(); 
$dom->loadHTMLfile("test.php"); 

//Gets the innerhtml value
$div = $dom->getElementById("box")->nodeValue;

//Writes it down in a file.
    $file = fopen("stuff.txt","w");
fwrite($file,$div);
fclose($file);

//Just for fast-checking if the code has any errors or not
    echo "File saved.";
}
?>

我想这个问题已经很清楚了。 这是如何获取CURRENT值而不是原始值的方法。

如果有帮助,请参见以下完整代码:

<html>
<head>
<script>
//The javascript function to add a "para" into a div with the id "box"

function addstuff() {
    var parag = document.createElement("P");        // Create a <button> element
var t = document.createTextNode("Lorem Ipsum");       // Create a text node
parag.appendChild(t);
    document.getElementById("box").appendChild(parag);
}
</script>
</head>
<body>



<!--Button to call the funtion-->
<button onclick="addstuff()">Add it</button>


<!--The form for the button to work-->
<form action="" method="post">

<!--The div to put the "para"s in. The style only adds borders-->
<div id="box" style="border: 2px solid black;">
<!--A pre-existing paragraph-->
<p>This was here before</p>

</div>

<!--The button to call the php-->
<input type="submit" name="use_button" value="Store in file" style="width:100%;" />
</form>

<!--The PHP-->
<?php
//Basically a function
if(isset($_POST["use_button"]))
{
    //Loads the file, which is named test.php
    $dom= new DOMDocument(); 
$dom->loadHTMLfile("test.php"); 

//Gets the innerhtml value
$div = $dom->getElementById("box")->nodeValue;

//Writes it down in a file.
    $file = fopen("stuff.txt","w");
fwrite($file,$div);
fclose($file);

//Just for fast-checking if the code has any errors or not
    echo "File saved.";
}
?>
</body>
</html>

这是不可能的:

  • PHP生成HTML,然后将其发送到浏览器。
  • 浏览器执行页面中的javascript。

浏览器中没有PHP! 服务器无法知道用户在浏览器中所做的任何事情!

您可以使用javascript进行AJAX调用,以将数据发送到服务器。

请参考以下答案: https : //stackoverflow.com/a/6009208/1275832

您似乎对<form>元素的工作<form>感到困惑。 仅将HTML代码添加到表单客户端不会在提交表单时将该HTML代码作为表单数据发送到服务器。 它也不会自动更新某些PHP文件。

您需要将HTML代码添加到<form>一部分的某些输入控件(输入,文本区域等)中。 然后,该输入的值将在提交时发送到服务器,此时您可以使用该发送的数据。

因此,在客户端,您可能有一个隐藏的输入,其中包含要发送的html。 每次需要更改html时都进行更新

的HTML

<form action="" method="post">
  <input id="boxinput" type="hidden" name="boxinput"/>
  <!--- Rest of form -->
</form>

JS

//cache these so you don't need to call getByElementById every call
var box = document.getElementById("box");
var boxinput = document.getElementById('boxinput');
function addstuff() {
  var parag = document.createElement("P");
  var t = document.createTextNode("Lorem Ipsum");
  parag.appendChild(t);
  box.appendChild(parag);
  //update the input field
  boxinput.value = box.innerHTML;  
}

然后在服务器端,从$ _POST全局获取输入数据并根据需要使用它

if(isset($_POST["use_button"])) {
  $boxHtml = $_POST['boxinput'];
  //..
  fwrite($file,$boxHtml);
  //..
}

强制性注释:如果要以某种方式使用此保存的html,例如在新页面上回显它,则应在保存/使用它之前对其进行清理。 或仅将其显示在沙盒框架中(有点像Stack Overflow如何对其沙盒片段进行沙盒处理)

这是不可能的。

但是您可以使用与phpjavascript中相同的query parameters来手动处理它

通过query parameters生成页面元素。

例如,当您获得test.php?div=box生成一个包含<div id='box'>

有这么多的解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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