I'm using a div with contenteditable="true"
as an input field, and now I'm looking for a way to pass the content of the div to a PHP variable in order to be able to save it in a .txt file. I've been looking around for solutions, and the only suggestion I've found so far would be to (as I understand it) add the content to a hidden input field before submitting it – though I'm not sure how to do this, it seems like there's probably a better way to approach this?
It's worth mentioning that I'm not that experienced working with back-end, so any explanation to a solution would be greatly appreciated – even just a pointer in the right direction at this time would be nice.
I'm currently having a regular form to test the save function with the variable $name
for texting purposes, but my goal is to save the text in the div in $textcontent
instead. (Preferably I'd use a href to submit instead of a button, but I doubt that makes much difference as to what method to use in order to solve my problem.)
index.php
<?php
//temporary, will be dynamic
$sessionid = "123testID";
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<div id="results">
<div name="textcontent" contenteditable="true" class="no-formatting">Content that should be saved to .txt file…</div>
<!-- for testing only -->
<form action="index.php" method="post">
<input type="text" placeholder="Name" name="name">
<input type="submit" name="submit">
</form>
<br>
<a href="index.php">reload</a>
<!----->
</div>
</body>
</html>
<?php
$name = $_POST["name"];
$sessionid = "123testID";
?>
<?php
if (strlen($name) > 0) {
$fp = fopen('stories/'.$sessionid.'.txt', 'a+');
fwrite($fp, "textcontent:\t".$name."\r\n");
fclose($fp);
}
?>