我正在用保存功能的定製所見即所得的編輯器,並通過保存功能我已經運行一些代碼在一定專區內搞定一切,將它保存到數據表或覆蓋它。但現在,我正試圖加載回頁面。試圖通過PHP
的過程如下:按下保存按鈕,它運行名爲save.php一個PHP腳本,如下所示。
我的問題是,我想它加載或回聲在原始html頁面上的某個div內的內容。我會怎麼做呢?基本上,我需要它像JavaScript的innerHTML函數一樣工作。
以下是我使用的文件,至少是相關的部分。
的test.html:
<form method="post" name="blog-post" id="blog-post">
<input type="hidden" name="postID" value="1"><!--Get the post's id-->
<div class="blog-editor-bar">
<a href="#" data-command='save'
onclick="submitForm('save.php');">
<i class='fa fa-save'></i>
</a>
</div>
<div id="blog-textarea" contenteditable>
</div>
<textarea style="display:none;" id="blog-post-cont" name="post-content"></textarea>
</form>
test.js:
function submitForm(action){
var theForm = document.getElementById("blog-post");
theForm.elements("post-content").value = document.getElementById("blog-textarea").innerHTML;
theForm.action = action;
theForm.submit();
}
save.php:
$conn = mysqli_connect('localhost', 'root', '', '');
if (mysqli_connect_errno()){
echo "<p>Connection Failed:".mysqli_connect_error()."</p>\n";
}
//store stuff in database
//Get Variables
$postid = $_POST['postID'] ? $_POST['postID'] : null;
$post = $_POST['post-content'] ? $_POST['post-content'] : null;
//if exists, overwrite
if($postid != null || $postid != ""){
$sqlSave = "SELECT * FROM wysiwyg.post WHERE idpost = $postid";
$rSave = mysqli_query($conn, $sqlSave) or die(mysqli_error($conn));
if(mysqli_num_rows($rSave)){
$sqlOverwrite = "INSERT INTO wysiwyg.post(post) VALUES(?) WHERE idpost = ?";
$stmt = mysqli_prepare($conn, $sqlOverwrite);
mysqli_stmt_bind_param($stmt, "sd", $post, $postid);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($conn);
} else {
newSave();
}
loadSave();
}
function newSave(){
$sqlNewSave = "INSERT INTO wysiwyg.post(post) VALUES(?)";
$stmt = mysqli_prepare($conn, $sqlNewSave);
mysqli_stmt_bind_param($stmt, "s", $post);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
mysqli_close($conn);
}
function loadSave(){
$sqlLoad = "SELECT * FROM wysiwyg.post WHERE idpost = $postid";
$rLoad = mysqli_query($conn, $sqlLoad) or die(mysqli_error($conn));
//This is the part I'm stuck on
}
謝謝大家提前幫助我!我已經堅持了至少幾個小時!
編輯:在人們評論SQL注入之前,我已考慮到它。這是我讓我的代碼在我的本地主機上工作之前,我運行了大量的反sql注入方法,我已經在過去做過。我提供的代碼僅對此功能很重要。
編輯#2:反注入代碼已經存在。我想我似乎忘記提供這些信息。我再說一遍,我在這裏提供的代碼只是與功能相關的代碼。我已經逃脫了字符串,修剪等等,但是這些代碼並不需要讓人們瞭解我正在嘗試做什麼。
你需要使用AJAX來保存文本。您的'$ sqlSave'和'$ sqlLoad'查詢也容易受到SQL注入的攻擊。您應該爲** all **用戶提供的數據使用綁定參數。 – Mike
有很多教程進入AJAX,https://learn.jquery.com/ajax/。您已經部分阻止了SQL注入。你應該到處去做,以後再做是個不錯的主意。 – chris85
感謝您的快速響應邁克。請檢查我的編輯。我知道SQL,但目前我首先關注功能,因此使其工作。我如何從一個PHP文件做Ajax,就像在我的加載php函數中一樣,我將如何調用Ajax調用來將值存儲在json文件中? – verdeletg