AJAX数据显示与PHP返回的不同变量

时间:2016-02-13 13:30:52

标签: php jquery ajax echo

我有PHP函数:

    public function add() {
    $image = $this->input->post('image');
    $headers = $this->travel_model->getFunction('headers');

    //Add new uploaded image
    if (!empty($_FILES['img']['name'])) :

        //Check uploaded file extension
        $file_parts = pathinfo($_FILES['img']['name']);
        $ext = $file_parts['extension'];

        if($ext == "jpg" || $ext == "jpeg" || $ext == "png") {
            $image = md5($_FILES['img']['name']) . "." . $ext;
            $img = ROOT.'resources/img/headers/'.$image;
            move_uploaded_file($_FILES['img']['tmp_name'], $img);
            $result = "passed";
        } else {
            $result = "error-img-format";
        }
    else :
        $result = "passed";
    endif;

    //Check if is new added slideshow or editable slideshow
    if($result != "error-img-format") {
        if($this->input->post('id')) {
            $this->headers_model->updateHeaders($image);
        } else {
            //Check if more than three slides and Delete the last slide
            foreach($headers as $key => $hdr) {
                if($key >= 2) {
                    $this->travel_model->deleteFunction($hdr->id, 'slideshows') ;
                }
            }
            $this->headers_model->insertHeaders($image);
        }
    }
    echo $result;
}

上传了一个扩展名不同于.jpg,.jpeg或.png的img,PHP显示:error-img-format应该这样做

JQuery的:

    $(document).ready(function () {
    $('#add_headers').submit(function (e) {
        e.preventDefault();
        $.ajax({
            type: 'post',
            url: '/backend/headers/add',
            data: $(this).serialize(),
            success: function (data) {
                alert(data);
            }
        });
        e.preventDefault();
    });
});

警报显示passed,与PHP不同。无法理解问题是什么......

0 个答案:

没有答案