-1

ajaxを使用して画像をアップロードしているときにページが更新されるのはなぜだろうと思っています。問題をデバッグしようとしましたが、見つかりませんでした。

これがコントローラー機能です。

public function uploadImage() {
    if(isset($_FILES['header_image'])) {
        $config['upload_path'] = 'public/images/global/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['file_name'] = 'b_'.random_string('alnum', 5);
        $this->upload->initialize($config);

        if($this->upload->do_upload('header_image')) {
            echo json_encode(array('status'=>"1", 'image_url'=>$this->upload->file_name));
        } else {
            $upload_error = array('error' => $this->upload->display_errors());
            echo json_encode(array('status'=>"0",'upload_error'=>strip_tags($upload_error['error'])));
        }
        exit;
    }
    $this->template->render();
}

私のビューのuploadImage.phpは次のようになりますが、

$(document).ready(function() {
$('#header_image').on('change', function(){
    $("#frm_global").ajaxForm({
        success: function(data) {
            res = $.parseJSON(data);
            if(res.status == 1){
                var html = "<img src='<?=base_url();?>path/"+res.image_url+"' class='preview' width='100' height='100' />";
                    html+= "<input type='hidden' id='image_url' name='image_url' value='"+res.image_url+"' >";
                    $("#preview").html(html);
                    $("#frm_global").ajaxFormUnbind();
            }
            if(res.status == 0){
                $("#frm_global").ajaxFormUnbind();
            }
        }
    }).submit();
});
});

<form name="frm_global" id="frm_global" enctype="multipart/form-data">
<div id="preview" style="float:left">
</div>
<div style="float:left">
    <div style="margin-top:25px">
        <input type="file" name="header_image" id="header_image"/>
        <input style="margin-left:100px" onclick="" type="image" id="upload_header" src="any path" />
    </div>
</div>
</form>
4

1 に答える 1

0

申し訳ありませんが、私はあなたの質問を適切に受け取っていないと思います。

しかし、送信するには、このように試すことができます

$("#theForm").ajaxForm({
url: 'server.php',
type: 'post',
success:function(data)
{
//do what you want to
}
});

役に立ちますか

于 2013-09-26T13:23:41.303 に答える