简体   繁体   中英

How to pass data on ajax with form and a value of a global variable?

I am uploading a file to my server and I want to save the name of the file to my database. I am able to save it to my database. I also want to save the name of the user together with the name of the file, but the problem is I cannot pass the name of the user to the controller from the blade because it is not included on the form of the button for uploading. The name of the user is a global variable, is there any possible way to pass it to ajax together with the form? What do I need to add on my code? Thank you in advance!

I have tried passing the username like this

data: "refNumber2="+refNumber2+formData,

but still it doesn't pass the refNumber.

This is my html code for file Uploading:

<form method="post" action="{{URL::to('/store')}} " enctype="multipart/form-data" id="frmuploadFile" class="frmuploadFile">

                            <input name="image" type="file" class="image">

                            <input type="hidden" name="_token" value="{{ csrf_token() }}">

                        <button type="submit" name="btn" id="btn-upload" class="btn btn-default col-sm-15">Upload a File</button>

                    </form>

This is my ajax:

$('form').on("submit",function(event) {
                event.preventDefault();
                var formData = new FormData($(this)[0]);
                $.ajax({
                    url: "{{ url ('/store')}}",
                    type: 'POST',      
                    data: "refNumber2="+refNumber2+formData,
                    async: true,
                    cache: false,
                    contentType: false,
                    enctype: 'multipart/form-data',
                    processData: false,   
                    success: function(data){
                    }
                });                
            });//form

This is My Controller:

public function store(Request $request){
        $refNumber = $request->get('refNumber2');
        if (Input::hasFile('image')){
            echo "UPLOADED <br>";
            $file = Input::file('image');

            $filenameWithExt = $request->file('image')->getClientOriginalName();
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            $extension = $request->file('image')->getClientOriginalExtension();

            $fileNameToStore = $filename.'.'.time().'.'.$extension;

            $file->move('uploads', $fileNameToStore);
            $filename = $file->getClientOriginalName();
        }

        DB::table('i_di_thread')
            ->insert(['refNumber'=>$refNumber,'message'=>$fileNameToStore]);
    }//Upload File

Just add another hidden field in your from and put your global variable value.

<form method="post" action="{{URL::to('/store')}} " enctype="multipart/form-data" id="frmuploadFile" class="frmuploadFile">

  <input name="image" type="file" class="image">

  <input type="hidden" name="_token" value="{{ csrf_token() }}">
  // add another hidden field here
  <input type="hidden" name="refNumber2" id="refNumber2" value="{{ your_global_variable }}">

  <button type="submit" name="btn" id="btn-upload" class="btn btn-default col-sm-15">Upload a File</button>

</form>

you can add another hidden field here nammed userName for exemple

  <input type="hidden" name="userName" id="userName" value="{{Auth::user()->name}}">

and in your Controller you can get it by

 $request->get('userName')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM