提交后,我的表单数据值未插入到我的数据库中。在使用ajax设置 ckEditor 发布数据之前,它成功完成了,但是此后,我从其他输入中得到了一个错误。对于我的所有输入,我得到的错误是Undefined index
。如果添加if (issest($_POST["submit"]))
,所有错误都会消失,但数据不会发送到数据库。你能帮助我吗?这是我的代码:
<form id="addpostForm" class="" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="inputPostTitle" class="sr-only">Title</label>
<input name="post_title" type="text" class="form-control" id="inputPostTitle" placeholder="Title">
</div>
<div class="form-group">
<label for="inputEditor" class="sr-only">Content</label>
<textarea name="post_content" type="text" class="form-control" id="inputEditor"></textarea>
</div>
<div class="form-group">
<label for="inputTags">Tags</label>
<input name="tags" type="text" id="inputTags" data-role="tagsinput" placeholder="Separate tags with (,) commas">
</div>
<button type="button" class="btn btn-primary" id="buttonAddPost">Submit</button>
</form>
JavaScript:
$(document).ready(function(){
$('#buttonAddPost').click(function(){
//var formData = new FormData($('#addpostForm')[0]);
var ckEditor = CKEDITOR.instances.inputEditor.getData();
formData = $("#addpostForm").serialize + '&ckEditor=' + encodeURIComponent(ckEditor);
//alert(ckEditor);
//formData.append("post_content", ckEditor);
$.ajax({
url: 'modules/post/add.php',
type: 'post',
data: formData,
cache: false,
processData: false,
success: function(resAddPost){
if(resAddPost == 'ok'){
$.bootstrapGrowl('Form data submitted successfully.', {type: 'success'});
}else{
$.bootstrapGrowl('Some problem occurred, please try again.', {type: 'danger'});
}
console.log(resAddPost);
},
error: function(xhr, ajaxOptions, thrownError){
console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
});
});
Php:
<?php include "../../../_includes/config.php"; ?>
<?php
include "../../data/prettyurl.php";
session_start();
$post_id = time();
$post_title = $_POST['post_title'];
$post_content = $_POST['post_content'];
$post_added = date('Y-m-d');
$post_author = $_SESSION['SES_LOGIN']['user_display'];
$tags = explode(",", $_POST['tags']);
$post_title = trim($post_title);
$post_title_result = $post_title;
if($post_title_result){
$post_slug = pretty_url($post_title);
$post_title_data = $post_title;
}
$post_slug2 = $post_slug;
$query_check_post_slug = mysql_query("SELECT * FROM tb_posts WHERE post_slug LIKE '".$post_slug."%'") or die(mysql_error());
while($row = mysql_fetch_assoc($query_check_post_slug)){
$post_slugs[] = $row['post_slug'];
if(mysql_num_rows($query_check_post_slug) !== 0 && in_array($post_slug, $post_slugs)){
$max = 0;
$post_slug = $post_slug2;
while(in_array(($post_slug.'-'.++$max), $post_slugs));
$post_slug .= '-'.$max;
}
}
$query_insert_post = mysql_query("INSERT INTO tb_posts (post_id, post_title, post_content, post_added, post_author, post_slug) VALUES ('$post_id', '$post_title', '$post_content', '$post_added', '$post_author', '$post_slug')") or die(mysql_error());
foreach($tags as $tag){
$tag_id = "Tag-".$tag;
$query_insert_tags = mysql_query("INSERT INTO tb_tags (tag_id, tag) VALUES ('$tag_id', '$tag')") or die(mysql_error());
$query_insert_tag_posts = mysql_query("INSERT INTO tb_tag_posts (post_id, tag_id) VALUES ('$post_id', '$tag_id')") or die(mysql_error());
}
if($query_insert_post){
echo "ok";
}else{
echo "err";
}
?>
我的ckEditor替换ID为“ inputEditor”。
答案 0 :(得分:0)
您不能这样做:
data: formData + '&ckEditor=' + ckEditor,
formData
不是字符串,您无法将其连接起来。
相反,您应该这样做:
formData.append('ckEditor', ckeditor);
在$.ajax
之前。
然后在PHP中使用$_POST['ckEditor']
来获取该字段。
您也可以像这样使用序列化数据:
formData = $("#addpostForm").serialize() + '&ckEditor=' + encodeURIComponent(ckEditor);
如果这样做,则不应使用contentType: false
。
答案 1 :(得分:0)
请更正您在$ _POST函数中添加的密钥名称。像这样
您使用以下数据发送数据:data:formData +'&ckEditor ='+ ckEditor,
那为什么要在$ _POST ['post_content'];
您需要尝试使用$ _POST ['ckEditor'];