在codeigniter中使用Ajax提交表单

时间:2016-02-22 07:27:43

标签: javascript jquery ajax codeigniter

我想在codeIgniter中使用ajax提交表单但是收到错误。

这是我的观看代码;

<div class="form-group input-control">
    <p id="error1" style="display: none; color: green"><b>Registered Successfully.</b></p>
    <p id="succ" style="display: none; color: red"><b>E-mail Alreay Registered.</b></p>
    <button class="button" onclick="newsletter()"><span class="icon-envelop"></span></button>
    <input type="text" class="form-control" placeholder="Your E-mail..." value="" id="newsletter" onblur="if (this.value == '') {this.value = 'Your E-mail...';}" onfocus="if(this.value == 'Your E-mail...') {this.value = '';}">
    <br>
    <p id="error" style="display: none; color: red"><b>Please Enter E-mail.</b>
        <p>
            <p id="error2" style="display: none; color: red"><b>Error while request..</b>
                <p>
</div>

这是我的剧本:

<script>
    function newsletter() {
        var email = $("#newsletter").val();
        if (email == "") {
            $("#error").css("display", "block");
        } else {
            $.ajax({
                type: "post",
                url: "<?php echo site_url();?>/shopit/index/newsletter",
                cache: false,
                data: 'search=' + email,
                success: function(response) {
                    if (response == "succ") {
                        $("#succ").css("display", "block");
                        $("#error").css("display", "none");
                        $("#error1").css("display", "none");
                        $("#error2").css("display", "none");
                    } else {
                        $("#succ").css("display", "none");
                        $("#error").css("display", "none");
                        $("#error1").css("display", "block");
                        $("#error2").css("display", "none");
                    }
                },
                error: function() {
                    $("#succ").css("display", "none");
                    $("#error").css("display", "none");
                    $("#error1").css("display", "none");
                    $("#error2").css("display", "block");
                }
            });
        }

    }
</script>

这是我的控制器功能代码;

public function newsletter(){
    $search=  $this->input->post('search');
    $result = $this->Index_model->newsletter($search);
    echo $result;
}

最后我的模特功能:

public function newsletter($search){
    $query = $this->db->get_where("shopit_newsletter", array('email'=>$search));
    $result = $query->result();

    if($query->num_rows() > 0){
            echo "error";
    }else{
        $data = array(
            "email"=>$search
        );
        $this->db->insert("shopit_newsletter", $data);
        echo "succ";
    }
}

这是抛出错误并导致错误,此代码出了什么问题,请帮助我,我已经google了,但没有找到确切的结果。

这是控制台快照。

console snapshot

1 个答案:

答案 0 :(得分:0)

首先,模型功能应该是

 public function newsletter($search)
{
    $query  = $this->db->get_where("shopit_newsletter", array('email'=>$search));
    $result = $query->result();    
    if($query->num_rows() > 0)
    {
        return "error";
    }
    $data = array(
        "email"=>$search
    );
    $this->db->insert("shopit_newsletter", $data);
    return "succ";        
}

控制器应该是

public function newsletter(){
    $search=  $this->input->post('search');
    $result = $this->Index_model->newsletter($search);
    echo json_encode(array("data"=>$result));
}

在ajax请求中

success: function(response) {
if (response.data == "succ") {