我正在尝试使用serialize方法向数据库提交不同的表单输入字段。我在textarea上使用tinyMCE。在研究了这个之后,我现在可以将所有字段提交到除textarea之外的数据库。我不知道它为什么不被序列化。我应该如何处理我的代码以将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 :(得分:1)
在您的点击事件中,首先获取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的自动保存插件
答案 1 :(得分:0)
试试这个
data = "In many countries food manufacturers are required by"
lines = data.split(' ')
print([(lines[i], lines[i+1]) for i in range(0, len(lines)-1, 2)])