PHP: How to get CURRENT innerHTML?

Simply put, I tried to create a site on which the user can create an element, and then put it in a div element with the id field. Js script works fine and p elements can be created.

And then I created a php script where it saves innerHTML in the "box" field and then saves it in a TXT file.

Now the problem is that the script returns innerHTML as its original value before p elements are added there.

Here is my php script:

 <?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.";
}
?>

I would suggest that the question is already pretty clear. Here's how to get the CURRENT value instead of ORIGINAL.

Here's all the code if it helps:

<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>
+4
source share
3 answers

It's impossible:

  • PHP HTML, .
  • javascript .

PHP! , !

AJAX javascript .

, : fooobar.com/questions/631539/...

+1

, , <form>. HTML- HTML- . PHP.

HTML (input, textarea ..), <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 global

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

: - html, , - , /. ( Qaru )

0

.

, query parameters, php javascript

query parameters.

For example, if you got test.php?div=boxto create a page containing<div id='box'>

There are so many solutions like

0
source

Source: https://habr.com/ru/post/1687796/


All Articles