1 回答

TA貢獻1817條經驗 獲得超6個贊
獲取innerHTML封閉<div>標簽的屬性
savedHTML = div.innerHTML;
// save to database
之后,使用適當的 CSS 添加<div>和</div>到您的字符串中,以將字符串輸入并存儲在數據庫中。
參見演示:
function Add() {
var div = document.createElement("DIV");
var p = document.createElement("P");
var line = document.createElement("HR");
var text = document.createElement("P");
div.className = 'container';
p.className = 'date';
p.id = 'demo';
line.className = 'line1';
text.id = "text";
text.className = "feedback-container-text";
document.body.appendChild(div);
div.appendChild(p);
div.appendChild(line);
div.appendChild(text);
document.getElementById("text").innerHTML = "some text";
var d = new Date();
d.getDay();
document.getElementById("demo").innerHTML = d;
savedHTML = div.innerHTML;
document.f["div-value"].value = savedHTML;
}
.container {
border-radius: 5px;
background-color: white;
padding: 20px;
width: 300px;
height: 220px;
float: left;
margin-left: 60px;
position: relative;
margin-bottom: 50px;
align-items: center;
justify-content: center;
box-shadow: 0 10px 12px 0 rgba(0, 0, 0, 0.13), 0 14px 30px 0 rgba(0, 0, 0, 0.09);
transition-duration: 0.2s;
}
.container:hover {
width: 300px;
height: 225px;
margin-top: -5px;
transition-duration: 0.2s;
}
<form name="f" action="">
<!--Change type from 'text' to 'hidden' to hide it from users-->
<!--and from 'button' to 'submit' so form would submit-->
<input name="div-value" type="text" value="">
<input name="add-button" type="button" id="add-button" value="Add DIV" onclick="Add()">
</form>
請注意,在實際情況下,該Add函數將通過將表單提交到以下 PHP 腳本來完成:
PHP 添加div到表
/*
* Following 2 PHP codes have been provided by the OP to benefit other community
* members who might find them useful.
*/
try {
// $dbh is connection to database
$div = $_GET['div-value'];
$div2 = '<div class="container">'.$div.'</div>';
$stmt = $dbh->prepare('INSERT INTO Feedback(Element) VALUES(:element)');
$stmt->bindParam(':element', $div2, PDO::PARAM_STR);
$stmt->execute();
echo "Insert successful!<br/>";
} catch (PDOException $e) {
echo "Error!: ", $e->getMessage(), "<br/>";
die();
}
并會使用以下 PHP 腳本檢索所有這些divs:
divPHP從表中檢索所有s
try {
// $dbh is also connection to database
$stmt = $dbh->prepare("SELECT Element FROM Feedback");
if ($stmt->execute()) {
while ($row = $stmt->fetch()) {
print_r($row["Element"]);
}
}
}
} catch (PDOException $e) {
echo "Error!: ", $e->getMessage(), "<br/>";
die();
}
- 1 回答
- 0 關注
- 95 瀏覽
添加回答
舉報