Php Codeigniter:如何在提交ajax表单后向用户返回成功/失败消息
我开始掌握Codeigniter的诀窍,但显然我在这里遗漏了一些东西 我不确定在提交ajax表单后如何将数据返回给用户。 以下是我代码中的相关部分,其中有一个问题: 查看:Php Codeigniter:如何在提交ajax表单后向用户返回成功/失败消息,php,jquery,ajax,codeigniter,Php,Jquery,Ajax,Codeigniter,我开始掌握Codeigniter的诀窍,但显然我在这里遗漏了一些东西 我不确定在提交ajax表单后如何将数据返回给用户。 以下是我代码中的相关部分,其中有一个问题: 查看: if (confirm("Are you sure?")) { var form_data = $('form').serialize(); $.post("<?php echo site_url();?>" + "/products/update_multiple",
if (confirm("Are you sure?"))
{
var form_data = $('form').serialize();
$.post("<?php echo site_url();?>" + "/products/update_multiple", (form_data),
// this is what I don't understand: how to populate the variable result with data returned from the model/controller?
function(result) {
$('#myDiv').html(result);
}
)
}
型号:
function update_multiple()
{
$this->load->model('products_model');
$this->products_model->updateMultiple();
// What goes here, to grab data from the model and send it back to the ajax caller?
}
function updateMultiple()
{
// I have a drop down list to select the field to update. The values of the drop down list look like this: "TableName-FieldName". Hence the explode.
$table_field = explode("-",$this->input->post('field'));
$table = $table_field[0];
$field = $table_field[1];
$data = $this->input->post('data');
$rows = explode("\n", $data);
foreach($rows as $key => $row)
{
$values = explode(" ", $row);
$arr[$key]["code"] = $values[0];
$arr[$key][$field] = $values[1];
}
if ($this->db->update_batch($table, $arr, 'code'))
{
// Here is the problem. What should I return here, so that it would be sent back to the calling ajax, to populate the result variable?
}
}
返回json
if ($this->db->update_batch($table, $arr, 'code'))
{
print json_encode(array("status"=>"success","message"=>"Your message here"));
}
确认函数中的jQuery
$.ajax({
type: "POST",
url: "<?php echo site_url();?>" + "/products/update_multiple",
data: $('form').serialize(),
dataType: "json",
success: function(content) {
if (content.status == "success") {
$('#myDiv').html(content.message);
}
}
});
$.ajax({
类型:“POST”,
url:“+”/products/update\u multiple”,
数据:$('form')。序列化(),
数据类型:“json”,
成功:功能(内容){
如果(content.status==“成功”){
$('#myDiv').html(content.message);
}
}
});
返回json
if ($this->db->update_batch($table, $arr, 'code'))
{
print json_encode(array("status"=>"success","message"=>"Your message here"));
}
确认函数中的jQuery
$.ajax({
type: "POST",
url: "<?php echo site_url();?>" + "/products/update_multiple",
data: $('form').serialize(),
dataType: "json",
success: function(content) {
if (content.status == "success") {
$('#myDiv').html(content.message);
}
}
});
$.ajax({
类型:“POST”,
url:“+”/products/update\u multiple”,
数据:$('form')。序列化(),
数据类型:“json”,
成功:功能(内容){
如果(content.status==“成功”){
$('#myDiv').html(content.message);
}
}
});
只要在控制器方法末尾回显“success”,您就会找到处理此问题的方法阅读此内容:只要在控制器方法末尾回显“success”,您就会找到处理此问题的方法阅读此内容:为什么不使用适当的HTTP代码,例如200来获得成功?所有代码的列表都可用,例如,是否真的相关?如果函数成功,那才是真正重要的不?如果没有,您可以始终返回错误,并给出这样做失败的原因。HTTP代码不会给出查询失败的原因请注意:jqXHR.success()、jqXHR.error()和jqXHR.complete()回调将在jQuery 1.8中被弃用。要准备代码以便最终删除,请使用jqXHR.done()、jqXHR.fail()和jqXHR.always()。@rick是否确定?每个代码都有解释。此外,您还可以添加自定义消息,不是吗?唯一的区别是,您不必在比较中包含status=>“success”和这个丑陋的字符串javascript@mkk这种字符串比较确实是不必要的。如果出现错误消息,已经有一个fail()
。为什么不使用正确的HTTP代码,比如200成功?所有代码的列表都可用,例如,是否真的相关?如果函数成功,那才是真正重要的不?如果没有,您可以始终返回错误,并给出这样做失败的原因。HTTP代码不会给出查询失败的原因请注意:jqXHR.success()、jqXHR.error()和jqXHR.complete()回调将在jQuery 1.8中被弃用。要准备代码以便最终删除,请使用jqXHR.done()、jqXHR.fail()和jqXHR.always()。@rick是否确定?每个代码都有解释。此外,您还可以添加自定义消息,不是吗?唯一的区别是,您不必在比较中包含status=>“success”和这个丑陋的字符串javascript@mkk这种字符串比较确实是不必要的。如果出现错误消息,则已经有一个fail()
。