2017-08-26 42 views
0

我正在嘗試使用序列化方法向數據庫提交不同的表單輸入字段。我在我的textarea上使用tinyMCE。在對此進行研究之後,我現在可以將所有字段提交到除textarea之外的數據庫。我不知道爲什麼它沒有被序列化。我應該怎樣做我的代碼以獲取textarea內容到數據庫?我的代碼如下所示使用序列化方法提交tinyMCE textarea到數據庫

<form id="myForm" method="post" action="myPage.php"> 
    <textarea class="tinymce" name="texteditor" id="texteditor"></textarea> 

    <input type="checkbox"name="get_value[]" value="A"> 
    <input type="checkbox"name="get_value[]" value="B"> 
    <input type="checkbox"name="get_value[]" value="C"> 
    <input type="checkbox"name="get_value[]" value="D"> 

    <select name="category" class="form-control" required> 
     <option>one</option> 
     <option>two</option> 
    </select> 
    <input name="points" type="text" class="form-control" /> 

    <input name="random" type="radio" value="no" /> No <br> 
    <input name="random" type="radio" value="yes" /> Yes 

    <button id="sub" name="upload"><b>Submit</b></button> 

</form> 

Java腳本

$("#sub").click(function() { 

    $.post($("#myForm").attr("action"), $("#myForm").serialize(), function(info){ $("#result").html(info); }); 
    clearInput(); 
    }); 

    $("#myForm").submit(function() { 
    return false; 
}); 

function clearInput() { 
    $("#myForm").each(function() { 
    $(this).val(''); 
    }); 
} 

PHP

if(!empty($_POST["get_value"])){ 
    foreach($_POST["get_value"] as $checkbox){ 
    } 
$question = $_POST['texteditor']; 
$random = $_POST['random']; 
$category = $_POST['category']; 
$points = $_POST['points']; 

$insert_question = "insert into questions (question,checkbox,random,category,points) values ('$question','$checkbox','$random','$category','$points')"; 

$run_question = mysqli_query($con, $insert_question); 
if($insert_question){ 
    echo "Question set successfully"; 
} 
else { 
    echo "failed"; 
} 
} 

else{ 
echo "<script>alert('Please select at least one option!')</script>"; 
} 
+0

那麼最新情況呢? fyi,如果你有任何值的話,你的代碼就會中斷,因爲它對SQL注入是開放的。 –

+1

你也應該檢查'if($ run_question){'not'if($ insert_question){',這就是爲什麼你在插入時得到誤報。 –

+0

對我無效 – Oponjous

回答

0

在你點擊事件首先得到tinymice的值,然後分配到文本區域 你可以使用任何以下方法

// Get the HTML contents of the currently active editor 
    var content = tinyMCE.activeEditor.getContent(); 

    // Get the raw contents of the currently active editor 
    var content =  tinyMCE.activeEditor.getContent({format : 'raw'}); 

    // Get content of a specific editor: 
    var content =  tinyMCE.get('content id').getContent() 

的小老鼠編輯器的值/內容,然後由jQuery的含量值分配給textarea的類似

$('textarea[name=textarea]').val(content); 

然後做序列化形式等的過程..

您也可以嘗試Tinymice

的自動保存插件
+0

感謝LogicBlower爲您提供快速響應。應用你的答案,我添加了var content = tinyMCE.activeEditor.getContent(); $(「#sub」)。click(function(){,但我應該在哪裏添加$('textarea [name = textarea]').val(內容);在我的代碼中,因爲我是新的jquery? – Oponjous

+0

它點擊(function(){var content = tinyMCE.activeEditor.getContent(); $('textarea [name = texteditor]')。val(content);謝謝 – Oponjous

+0

很高興,它的工作,你可以Upvote我的答案,以便其他人知道問題已得到解決。 – LogicBlower

0

試試這個

$("#sub").click(function(e) { 
     e.preventDefault();//it will prevent ur form submit by default 

     $.post($("#myForm").attr("action"), $("#myForm").serialize(), function(info){ $("#result").html(info); }); 
     clearInput(); 
     }); 

     $("#myForm").submit(function() { 
     return false; 
    }); 

function clearInput() { 
    $("#myForm").each(function() { 
    $(this).val(''); 
    }); 
}