Search code examples
phplaravellaravel-6laravel-route

How to redirect from one blade view to another blade view, and pass dynamically generated variable to it?


In my view , let's call it View-A, I get an input from user in a prompt and assign it to a variable, and then I need to load in browser another view, say View B, while passing it that variable.

Say, in my viewA.blade.php, I have taken user's input and assigned it to variable usersInput. Now I need to send it to view B, whose route in web.php is defined at Route::get('editRecord', 'MyController@displayEditRecordView')->name('editRecordFormView');.

Question is how do I load route(editRecordFormView) in browser and pass usersInput variable to it, from javascript written in my blade view?


@Tushar In in my ViewA.blade.php:

$.ajax({
    url: url_editRecordView.url,
    data: {
        usersInput: dataToSend.usersInput,
    },
    method: "get",
    cache: false,
    success: function (dataReturned, stringStatus, jqXHR) {
        console.log("dataReturned from mYController.displayEditRecordFormView:-");
        console.log(dataReturned);//check
        console.log("Type of dataReturned:-" + typeof dataReturned); //SAME DATA COMES BACK, AS THE DATA I SENT
    },
    error: function (jqXHR, stringStatus, stringExceptionThrown) {
        alert("Error! status: " + stringStatus + " ExceptionThrown: " + stringExceptionThrown);//check
    }
});

In my MyController:

public function displayEditRecordFormView (Request $request) {
    error_log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");//check
    error_log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");//check
    $title= $request->input('usersInput');
    error_log("In displayEditRecordFormView: title: ".$title);//check
    $allPaintingsArray = $this->getAllPaintingsArraySanitized();
    $data = [
        "allPaintingsArray"=>$allPaintingsArray ,
        "title"=>$title
    ];

    return view("paintings.editRecordForm")->with("allGoodData", $data);
}

Solution

  • Instead of AJAX call why don't use something like this:

    var url = "/editRecord?usersInput=dataToSend.usersInput;
    window.location.href = url;
    

    catch variable In controller:

    $title= request()->filled('usersInput');// or find a way according to Laravel version