我正在创建一些功能,在提交表单时,会显示一条成功消息,并通过AJAX获取更新的详细信息并显示在页面上。
控制器:
public function updateAction()
{
$query="my query"
if(mysql_query($query))
{
echo "Your changes have been saved successfully";
$customer=Customer::getDetails($_POST['cust_id']);
include('application/view/admin/customer/_view.php');
}
else
{
echo "There was a problem saving your changes";
}
}
视图:
$('#update-customer-form').submit(function(){
$.ajax({
url: 'admin/customer/update',
type: 'POST',
data: $(this).serialize(),
success: function(result){
alert(result);
$('#details').html(result);
}
});
return false;
});
如果记录保存成功,我想传递成功消息,以及_view.php文件的HTML输出。否则,只显示一条错误消息。我该怎么做?
编辑:除了使用json_encode()之外,还有其他方法可以做到这一点吗?我遇到的问题是在json_encode()中包含PHP文件。
返回一个具有两个属性的json对象:
{ "success": "true", "markup":"<div>markup goes here</div>" }
请记住在标记中对引号进行编码。
success: function(result){
if (result.success=='true') {
$('#details').html(result.markup);
}
}