Home > Enterprise >  laravel link href click to save some data
laravel link href click to save some data

Time:02-20

  
 <a href="https://localhost/sample.pdf" >Preview  Your File</a>

here link click to open pdf its fine but i want to insert some data when click this link how to insert and how to pass data laravel

CodePudding user response:

You can do this in two methods

1: Using Ajax: First insert your data then in the success redirect to your file .

2: using controller to redirect you to the file use form to submit the data you want to controller then via controller redirect it to your file after adding data to database

something like this

    public function AddDataToDatabase(Request $request)
    {

        $datas=New DataModal();
        $datas->name = $request->name;
        $datas->address = $request->address;
        $datas->lastName = $request->lastName;
        $datas->email = $request->email;
        $data->save();


        $items=Attachments::where('id',1)->get();
        return redirect('Attachments/'.$items[0]->attachmentName);
    }

or Using Ajax

            $.ajax({
                type: "POST",
                url: your_URL,
                data: {
                    'name':'your_name',
                    'id':'your_id',
                    'email':'your_email',
                },
                success: function(data) {
                            window.location = 'https://localhost/sample.pdf';
                },
                error: function(e) {
                    console.log(e);
                }
            });

then in your controller create a new method like this:

    public function AddDataToDatabase(Request $request)
    {

        $datas=New DataModal();
        $datas->name = $request->name;
        $datas->address = $request->address;
        $datas->lastName = $request->lastName;
        $datas->email = $request->email;
        $data->save();

        return response()->json(['msg' => 'added successfully'],200);
    }

CodePudding user response:

Instead of a link, create form that contains the fields that you want to store in database. Make the fields hidden if they won't be filled by the user. Then submit the form to a controller. Store the data, redirect to a new path that will display the pdf.

CodePudding user response:

You can use Post HTTP request or if you want to use link the link,use the Get HTTP request.Click here

  • Related