如何使用带有codeigniter的jquery ajax方法(post)正确提交表单

时间:2022-03-17 19:20:42

I've build a small CMS in Codeigniter for a website with a couple of forms. The forms are all submitted with the ajax method in jQuery and passed to the controller. All works fine but I figured it out that I always get a success, no matter if the data is saved in the database or not. The method only checks if the data is passed to the controller properly, i guess. On success there is a message for the user (Saved), this will fire everytime, no matter what happens behind the scenes. How I can force success (or done, or some other callback) to get the "Saved" message only if the data is saved?

我在Codeigniter中为一个有几种形式的网站建立了一个小型CMS。表单都是使用jQuery中的ajax方法提交的,并传递给控制器​​。一切正常,但我发现无论数据是否保存在数据库中,我总能获得成功。我想这个方法只检查数据是否正确传递给控制器​​。成功时,有一条消息给用户(已保存),无论幕后发生什么,这都会触发。只有在保存数据的情况下,我才能强制成功(或完成或其他一些回调)获取“已保存”消息?

Here is the code:

这是代码:

//javascript
$("#save_club").click(function(){

    var club_name = $('input[name="club_name"]').val();
    var location = $('input[name="location"]').val();
    var phone = $('input[name="phone"]').val();

    $.ajax({
        type: "post",
        url: base_url + "/clubs/save_club/",
        data: {club_name:club_name,location:location,phone1:phone1},
        success:function(data)
        {
            $('input[type="submit"]').attr("value", "Saved!");
            $('input[type="submit"]').css("background-color", "#32c310");
            $('input[type="submit"]').css("cursor", "default");
            $(".dynamic_content").load(base_url + "/clubs/clubs_list");
        }
    });     
});

//controller
public function save_club(){
    $newdata = array();
    $newdata['club_name'] = $this->input->post("club_name");
    $newdata['location'] = $this->input->post("location");
    $newdata['phone'] = $this->input->post("phone");
    $this->load->model("model_save");
    $this->model_save->save_club_to_db($newdata);
}

1 个解决方案

#1


0  

You first would need to validate the inputs with PHP. For example:

首先需要使用PHP验证输入。例如:

<?php
$success = 'true';
foreach($_POST as $input) {
  if ($input == '') {
    $success = 'false';
    break;
  }
}
echo $success;
?>

Next, check the return with the data var you passed in the success function:

接下来,使用在success函数中传递的数据var检查返回:

$.ajax({
    type: "post",
    url: base_url + "/clubs/save_club/",
    data: {club_name:club_name,location:location,phone1:phone1},
    success:function(data)
    //Whatever PHP echoes in the script gets put into the variable data
    {
        var result = (data ? 'saved!' : 'error!'); //data should either be true or false
        $('input[type="submit"]').attr("value", result);
        $('input[type="submit"]').css("background-color", "#32c310");
        $('input[type="submit"]').css("cursor", "default");
        $(".dynamic_content").load(base_url + "/clubs/clubs_list");
    }
}); 

#1


0  

You first would need to validate the inputs with PHP. For example:

首先需要使用PHP验证输入。例如:

<?php
$success = 'true';
foreach($_POST as $input) {
  if ($input == '') {
    $success = 'false';
    break;
  }
}
echo $success;
?>

Next, check the return with the data var you passed in the success function:

接下来,使用在success函数中传递的数据var检查返回:

$.ajax({
    type: "post",
    url: base_url + "/clubs/save_club/",
    data: {club_name:club_name,location:location,phone1:phone1},
    success:function(data)
    //Whatever PHP echoes in the script gets put into the variable data
    {
        var result = (data ? 'saved!' : 'error!'); //data should either be true or false
        $('input[type="submit"]').attr("value", result);
        $('input[type="submit"]').css("background-color", "#32c310");
        $('input[type="submit"]').css("cursor", "default");
        $(".dynamic_content").load(base_url + "/clubs/clubs_list");
    }
});