PHP:如何获得CURRENT innerHTML?

时间:2017-10-19 12:25:34

标签: javascript php html innerhtml

简单地说,我试图创建一个用户可以创建元素的网站,然后将其放在带有id" box"的div元素中。 js脚本完美无缺,可以创建p元素。

然后,我创建了一个php脚本,它保存了" box"的内部HTML。 div然后将其保存在.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值而不是ORIGINAL值。

如果有帮助,请点击整个代码:

<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>

3 个答案:

答案 0 :(得分:1)

这是不可能的:

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

浏览器中没有PHP!并且服务器无法知道用户在浏览器中做了什么!!

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

请参阅此答案:https://stackoverflow.com/a/6009208/1275832

答案 1 :(得分:0)

您似乎对<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如何对其堆栈片段进行沙盒化)

答案 2 :(得分:0)

这是不可能的。

但您可以使用query parametersphp

中相同的javascript手动处理

query parameters生成您的页面元素。

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

的网页时

有这么多解决方案