我有一个带有两个输入字段title和postbody的模态我如何将值插入到我的数据库

时间:2017-09-19 07:10:11

标签: php jquery ajax codeigniter

这是我的模态视图表单操作发送到Post / addPost

<div class="modal fade" id="addPostModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header btn-primary">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <form id="createPost" action="Post/addPost" method="post">
          <div class="form-group">
            <label for="title">Title</label>
            <input type="text" class="form-control" id="title" name="title" placeholder="Enter title">
          </div>
          <div class="form-group">
            <label for="postbody">Body</label>
            <textarea class="form-control" id="postBody" name="postBody" rows="3"></textarea>
          </div>

      </div>
      <div class="modal-footer">

        <button type="submit" class="btn btn-primary">Submit Post</button>
        </form>
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

这是我的Post / addPost控制器

public function addPost()
{
        //$this->load->library('form_validation');
        $validator = array('success' => false, 'messages' => array());

        $validate_data = array(
            array(
                'field' => 'title',
                'label' => 'Title',
                'rules' => 'required'
            ),
            array(
                'field' => 'postBody',
                'label' => 'Body',
                'rules' => 'required'
            )
        );

        $this->form_validation->set_rules($validate_data);
        $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');


        if($this->form_validation->run() === true) {

            $this->load->model('Post_model');

            if ($query = $this->Post_model->addPosts()) {

                $validator['success'] = true;
                $validator['messages'] = 'Successfully Posted';
            } else {
                $validator['success'] = false;
                $validator['messages'] = 'error';
            }

        } else {
            $validator['success'] = false;
            foreach ($_POST as $key => $value) {
                $validator['messages'][$key] = form_error($key);
            }
        }

        echo json_encode($validator);
}

这是我的Post_model-&gt; addPosts

public function addPosts()
    {
        $new_post_insert_data = array(
            'post_title' => $this->input->post('title'),
            'post_body' => $this->input->post('postBody')
            );

        $insert = $this->db->insert('posts', $new_post_insert_data);
        return $insert;

    }

这是我的jquery ajax代码

$(document).ready(function() {
    $("#createPost").unbind('submit').bind('submit', function() {
        var form = $(this);

        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            data: form.serialize(),
            dataType: 'json',
            success:function(response) { 

                if(response.success == true) {
                    $("#messages").html('<div class="alert alert-success alert-dismissible" role="alert">'+
                      '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>'+
                      response.messages+
                    '</div>');
                    $('#registrationsuccess').html('Succesfully Registered Please Login').fadeIn('slow').delay(9000).fadeOut('slow');
                    $('#btnRegister').remove();

                    $("#signinForm")[0].reset();
                    $(".text-danger").remove();
                    $(".form-group").removeClass('has-error').removeClass('has-success');


                } else {
                    alert(response.success)
                    $.each(response.messages, function(index, value) {
                        var element = $("#"+index);

                        $(element)
                        .closest('.form-group')
                        .removeClass('has-error')
                        .removeClass('has-success')
                        .addClass(value.length > 0 ? 'has-error' : 'has-success')
                        .find('.text-danger').remove();

                        $(element).after(value);


                    });

                }
            } // /success
        });  // /ajax

        return false;
    }); 
});

我将此脚本链接到我的页脚上

<script src="<?php echo base_url("custom/js/post.js"); ?>"></script>

但是当我点击我的模态上的提交帖子按钮并检查控制台为什么它会转到另一个控制器它转到用户/注册它是假设去Post / addPost,因为这是我在我的行动表格上的内容在我的模态上可能是错的,因为我在检查控制台时没有看到任何错误

0 个答案:

没有答案