0
我想創建一個簡單的表單提交數據與數據庫的Js助手。下面是這個視圖:CakePHP 2.1和Ajax表單提交與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');
}
}
}
產生的提交從我的表單創建值的數據庫中的記錄,但沒有通過。任何見解?
謝謝!完美的作品。 – randomswathe 2012-04-19 15:12:24