Click to upload (Hide upload form)

HTML

<button type="button" id="change_id_copy">更新正面</button>
<button type="button" id="change_id_copy2">更新反面</button>


<form id="form_id_front" name="form_id_front" novalidate action="/admin/ajax/member/id_copy/front" method="post" style="height: 0px; overflow: hidden;">
	<input type="file" name="file" id="id_copy">
	<input type="hidden" name="member_id" value="">
</form>

<form id="form_id_back" name="form_id_back" novalidate action="/admin/ajax/member/id_copy/back" method="post" style="height: 0px; overflow: hidden;">
	<input type="file" name="file" id="id_copy">
	<input type="hidden" name="member_id" value="">
</form>

Javascript

var id_copy_app = function(){
    var $c = null;
    var $form_front = null;
    var $form_back = null;
    var member_id = 0;

    function init(){
        $c = $('#md_member');
        $form_front = $c.find('#form_id_front');
        $form_back = $c.find('#form_id_back');

        $form_front.ajaxForm({
            dataType: 'json',
            beforeSubmit: function(arr, $form, options) {
            },
            success: function(data, statusText, xhr, $form){
              //console.log(data);
              if(typeof data.status == 'undefined'){
                    alert(data);
              }else if(data.status == 'fail'){
                    alert(data.message);
              }else{
                    //console.log(data);
                    alert('修改成功');
                    var $link = $c.find('.id_img.id_copy');
                    $link.attr('href', data.data);
                    $link.find('img').attr('src', data.data);
              }
            },
            error: function(data){
                console.log(data);
            }
        });

        $form_back.ajaxForm({
            dataType: 'json',
            beforeSubmit: function(arr, $form, options) {
            },
            success: function(data, statusText, xhr, $form){
              //console.log(data);
              if(typeof data.status == 'undefined'){
                    alert(data);
              }else if(data.status == 'fail'){
                    alert(data.message);
              }else{
                    //console.log(data);
                    alert('修改成功');
                    var $link = $c.find('.id_img.id_copy2');
                    $link.attr('href', data.data);
                    $link.find('img').attr('src', data.data);
              }
            },
            error: function(data){
                console.log(data);
            }
        });

        $c.find('#change_id_copy').on('click', function(){
            $form_front.find('#id_copy').trigger('click');
        });

        $form_front.find('#id_copy').on('change', function(){
            //alert('ok');
            $form_front.submit();
        });

        $c.find('#change_id_copy2').on('click', function(){
            $form_back.find('#id_copy').trigger('click');
        });

        $form_back.find('#id_copy').on('change', function(){
            //alert('ok');
            $form_back.submit();
        });
    }

    function set_id(_id){
        //$form.find('[type="text"]').val('');
        member_id = _id;
        $c.find('form [name="member_id"]').val(member_id);
    }

    function show_response_error(errors){
        var str = '';
        //console.log(errors);

        for(var key in errors){
            var err = errors[key];
            str += err[0] + '\n';
        }
        
        alert(str);
    }

    return {
        init: init, 
        set_id: set_id
    }
}();

Laravel

public function update_id_copy_front(Request $req){
	$this->check_login();

	$member_id = $req->input('member_id');
	if($member_id == false){
		$this->fail_response(0, '錯誤的會員代碼');
	}

	$file = $req->file('file');
	if($file == false){
		$this->fail_response(0, '請選擇上傳檔案');
	}
	//logg($file);
	$path = $file->store('public/' . $member_id . '/id_copy');
	$path = str_replace('public', '', $path);
	//logg($path);

	$member = MemberM::find($member_id);
	$member->id_copy = $path;
	$member->save();

	$this->success_response(img_url($path));
}