首页 > 解决方案 > 如何使用ajax修复未插入数据库的值

问题描述

提交后,我的表单数据值没有插入到我的数据库中。在我使用 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”。

标签: phphtmlajax

解决方案


你不能这样做:

data: formData + '&ckEditor=' + ckEditor,

formData不是一个字符串,你不能连接它。

相反,您应该这样做:

formData.append('ckEditor', ckeditor);

之前$.ajax

然后在PHP中使用$_POST['ckEditor']来获取这个字段。

您还可以像这样使用序列化数据:

formData = $("#addpostForm").serialize() + '&ckEditor=' + encodeURIComponent(ckEditor);

如果你这样做,你不应该使用contentType: false.


推荐阅读