CakePHP 2.1和Ajax表单与Js Helper一起提交

时间:2012-04-18 17:48:47

标签: jquery ajax cakephp-2.1

我正在尝试创建一个简单的表单,使用Js帮助器将数据提交到数据库。以下是观点:

<div class="modal hide fade" id="modal-note-add">
  <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3>Add Note</h3>
  </div>
  <div id="modal-note-add-body" class="modal-body">
    <?php echo $this->Form->create('Note', array('action' => 'addIframe')); ?>
    <?php
    echo $this->Form->input('id');
    echo $this->Form->input('project_id', array('type' => 'hidden'));
    echo $this->Form->input('user_id', array('type' => 'hidden', 'value' => $this->Session->read('Auth.User.id')));
    echo $this->Form->input('date', array('type' => 'text', 'value' => date('Y-m-d'), 'class' => 'datepicker'));
    echo $this->Form->input('note_type_id', array('type' => 'hidden', 'value' => 1));
    echo $this->Form->input('comment', array('class' => 'tinymce span4'));
    ?>
  </div>
  <div class="modal-footer">
    <?php
    echo $this->Js->submit('Save', array('url' => '/notes/addIframe', 'update' => '#content', 'class' => 'btn btn-primary btn-large'));
    echo $this->Form->end();
    ?>
  </div>
</div>

输出:

<div class="modal hide fade in" id="modal-note-add" style="display: block; ">
  <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3>Add Note</h3>
  </div>
  <div id="modal-note-add-body" class="modal-body">
    <form action="/notes/addIframe" id="NoteAddIframeForm" method="post" accept-charset="utf-8">
    <div style="display:none;">
      <input type="hidden" name="_method" value="POST">
    </div>
    <input type="hidden" name="data[Note][id]" id="NoteId">
    <input type="hidden" name="data[Note][project_id]" id="NoteProjectId" value="11095">
    <input type="hidden" name="data[Note][user_id]" value="1" id="NoteUserId">
    <div class="input text">
      <label for="NoteDate">Date</label>
      <input name="data[Note][date]" value="2012-04-18" class="datepicker hasDatepicker" type="text" id="NoteDate">
    </div>
    <input type="hidden" name="data[Note][note_type_id]" value="1" id="NoteNoteTypeId">
    <div class="input textarea">
      <label for="NoteComment">Comment</label>
      <textarea name="data[Note][comment]" class="tinymce span4" cols="30" rows="6" id="NoteComment"></textarea>
    </div>
    </form>
  </div>
  <div class="modal-footer">
    <div class="submit"><input class="btn btn-primary btn-large" id="submit-1899329211" type="submit" value="Save"></div>
  </div>
</div>
....
<script type="text/javascript">
//<![CDATA[
$(document).ready(function () {$("#submit-1899329211").bind("click", function (event) {$.ajax({data:$("#submit-1899329211").closest("form").serialize(), dataType:"html", success:function (data, textStatus) {$("#content").html(data);}, type:"post", url:"\/notes\/addIframe"});
return false;});});
//]]>
</script>

控制器操作:

public function addIframe() {
  if ($this->request->is('ajax')) {
  $this->Note->save($this->request->data);
  if ($this->Note->save($this->request->data)) {
      $this->Session->setFlash(__('The project note has been saved.', true), 'flash_success');
    } else {
     $this->Session->setFlash(__('The note could not be saved. Please, try again.', true), 'flash_error');
    }
  }
}

生成的提交会在数据库中创建一条记录,但不会传递我的表单中的任何值。有什么见解吗?

1 个答案:

答案 0 :(得分:1)

这是一篇类似的帖子Ajax form submit CakePHP 2。希望这会有所帮助。