PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 15, 2022

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

 November 15, 2022     laravel, laravel-6, laravel-route, php     No comments   

Issue

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


Answered By - ashanrupasinghe
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing