简体   繁体   English

如何从一个刀片视图重定向到另一个刀片视图,并将动态生成的变量传递给它?

[英]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.在我看来,我们称它为 View-A,我在提示符中从用户那里获得输入并将其分配给一个变量,然后我需要在浏览器中加载另一个视图,比如视图 B,同时将那个变量传递给它。

Say, in my viewA.blade.php , I have taken user's input and assigned it to variable usersInput .比如说,在我的viewA.blade.php中,我接受了用户的输入并将其分配给变量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');现在我需要将它发送到视图 B,它在 web.php 中的路由定义在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?问题是我如何在浏览器中加载路由( editRecordFormView )并将usersInput变量从我的刀片视图中写入的 javascript 传递给它?


@Tushar In in my ViewA.blade.php: @Tushar在我的 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:在我的 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);
}

Instead of AJAX call why don't use something like this:而不是 AJAX 调用为什么不使用这样的东西:

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

catch variable In controller:在 controller 中捕获变量:

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

You can make use of AJAX.您可以使用 AJAX。 Just set an event handler for when the user gives an input, and send a request containing the user input to a method in your View-A controller via AJAX and from there you can redirect to View-B along with the value that you got from the request.只需为用户提供输入时设置一个事件处理程序,然后通过 AJAX 将包含用户输入的请求发送到 View-A controller 中的方法,然后您可以从那里重定向到 View-B 以及您从中获得的值请求。

EDITED已编辑

Instead of using view() method try using redirect() method like this:不要使用view() 方法,而是尝试使用如下所示的redirect() 方法

return redirect()->route('editRecordFormView', ['input' => 'userInput']);

To know more about redirects please refer tothis要了解有关重定向的更多信息,请参阅

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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