PHP: 如何获取当前的 innerHTML?
PHP: How to get CURRENT innerHTML?
简单来说,我尝试做一个网站,用户可以在其中制作一个元素,然后把它放在一个id为"box"的div元素中。 js脚本完美运行,可以创建p个元素
然后,我制作了一个 php 脚本,用于保存 "box" div 的 innerHTML,然后将其保存在 .txt 文件中。
现在的问题是,脚本 returns innerHTML 值作为原始值,在添加 p 元素之前。
这是我的 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.";
}
?>
我想问题已经很清楚了。这是获取当前值而不是原始值的方法。
如果对您有帮助,请查看完整代码:
<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 调用以将数据发送到服务器。
请参考这个回答:
您似乎对 <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,例如在某个新页面上回显它,您应该在 saving/using 之前对其进行清理。或者仅在沙盒框架中显示它(有点像 Stack Overflow 如何对其 Stack Snippets 进行沙盒处理)
这是不可能的。
但是您可以使用 query parameters
手动处理它,这在 php
和 javascript
中是相同的
通过 query parameters
生成您的页面元素。
例如,当您 test.php?div=box
生成一个包含 <div id='box'>
的页面时
像这样的解决方案太多了
简单来说,我尝试做一个网站,用户可以在其中制作一个元素,然后把它放在一个id为"box"的div元素中。 js脚本完美运行,可以创建p个元素
然后,我制作了一个 php 脚本,用于保存 "box" div 的 innerHTML,然后将其保存在 .txt 文件中。
现在的问题是,脚本 returns innerHTML 值作为原始值,在添加 p 元素之前。
这是我的 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.";
}
?>
我想问题已经很清楚了。这是获取当前值而不是原始值的方法。
如果对您有帮助,请查看完整代码:
<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 调用以将数据发送到服务器。
请参考这个回答:
您似乎对 <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,例如在某个新页面上回显它,您应该在 saving/using 之前对其进行清理。或者仅在沙盒框架中显示它(有点像 Stack Overflow 如何对其 Stack Snippets 进行沙盒处理)
这是不可能的。
但是您可以使用 query parameters
手动处理它,这在 php
和 javascript
通过 query parameters
生成您的页面元素。
例如,当您 test.php?div=box
生成一个包含 <div id='box'>
像这样的解决方案太多了