如何在不刷新的情况下创建jQuery + ajax表单?这是我的控制器和视图:
http://pastebin.com/GL5xVXFZ
在"清除"PHP 中,我创建了这样的东西:
$(document).ready(function(){
$("form#submit").submit(function() {
var note = $('#note').attr('value');
$.ajax({
type: "POST",
url: "add.php",
data: "note="+ note,
success: function(){
$('form#submit').hide(function(){$('div.success').fadeIn();});
}
});
return false;
});
});
在add中.php文件是插入到数据库。
有更复杂的方法可以做到这一点,例如在您的操作中检测 ajax 请求,然后在检测到时打印出 javascript 响应。这样做的方式是
JAVASCRIPT
function postForm(note){
$.ajax({
url : '/controller/action',
type : 'POST',
data : 'note='+note,
success : function(jsn){
var json = $.parseJSON(jsn);
if(json.status == 200)
alert('Completed Successfully');
else
alert('Not Completed Successfully');
},
error : function(xhr){
//Debugging
console.log(xhr);
}
});
}
.PHP
<?php
Class Controller_ControllerName extends Controller_Template{
public $template = 'template';
public function action_index(){}
public function action_form(){
$this->auto_render = false; // <-EDITED
$array = array();
//PROCESSING FORM CODE HERE
if($success){
$array['status'] = 200;
}else{
$array['status'] = 500;
}
print json_encode($array);
}
}
?>
这是我在没有测试的情况下完成的示例,但这肯定足以让您工作