Laravel 5 Passing data from Controller to View with AJAX - php

I'm having an issue passing data from controller to a view loaded via ajax. Below is my code:
JS:
function editass(id, name, title) {
var modal_title = document.getElementById("modal-title");
var modal_name = document.getElementById("modal-name");
var modal_body = document.getElementById("modal-body");
modal_title.innerHTML=title;
modal_name.innerHTML=name;
modal_body.innerHTML = '<div class = "text-info" align="center">Just a moment...</div>';
$("#modal-body").load('/SchoolSmart/public/assessments/edit/'+id);
//alert("modal");
}
Routes:
Route::get('/assessments/edit/{id}', 'AssessmentsController#edit');
Controller function
public function edit($id)
{
$editassessments = Assessment::findOrFail($id);
return view('assessments.edit')->with('$editassessments', $editassessments);
}
View
Assessment name: {!! $editassessments->name !!}
The error returned from the header is:
Undefined variable: editassessments
The view loads fine if I remove the call to variable, which is the whole essence of the page. Also, this function works fine if I the view was not loaded via AJAX.
Please, help on how I pass the variable from controller to view loaded via AJAX.
Thank you

Remove the '$':
return view('assessments.edit')->with('editassessments', $editassessments);

Related

Laravel send data to multiple page

In laravel 7, I am getting data from db from a single route:
Route::get('/connection_view_details/{Cid}','ConnectionController#view_details');
I want to send the data to multiple page:
public function view_details($Cid)
{
$Cid = base64_decode($Cid);
$network_details = DB::table('connection_network_details')
->where('Connection_id',$Cid)
->first();
return view('connection.view_details',compact('network_details'));
return view('connection.connection_detail_tables.network_details',compact('network_details'));
}
I want to retuen view of view_details page ,not the network_details page.but I want to send the variable network_details in the both pages.
You can use the view composers
use Illuminate\Support\Facades\View;
public function view_details($Cid)
{
$Cid = base64_decode($Cid);
$network_details = DB::table('connection_network_details')
->where('Connection_id',$Cid)
->first();
View::composer('connection.connection_detail_tables.network_details', function ($view) use ($network_details) {
$view->with('network_details', $network_details);
});
return view('connection.view_details',compact('network_details'));
}
But you only can use that variable in that view only.

Write in DB doesn't work in Controller, but works in view - Laravel

I'm trying to write some data in a DB after clicking on a save button.
During my development I was writing successfully in my DB with PHP code in my view, but the PHP code was ran when the page was loaded and not when my button was clicked.
What I did is a redirection to an address that call a function in my controller and then, that redirect to a login page.
Here is my controller's code:
<?php
namespace App\Http\Controllers;
use JavaScript;
use Illuminate\Support\Facades\DB;
class bus_confirmationController extends Controller{
public function test(){
$results = DB::select('select bus_direction from bus_line where card_uid = "0xcb009299"', array(1));
JavaScript::put([
'selectionData' => $results
]);
return view('bus_confirmation');
}
public function postRequest(){
DB::table('bus_line')->insert(
array('ID' => '', 'bus_direction' => '77t', 'card_uid' => '0xcb009299')
);
return view('home');
}
}
Here is my view's code:
[...]
<div class="panel panel-default" id="save" onclick="savedata()" >
SAVE
</div>
[...]
<script type="text/javascript">
function savedata() {
var toReturn = [];
var data = ["88t", "85t","77t","redLinet"];
for (i = 0; i < data.length; i++) {
var image = document.getElementById(data[i]).style.visibility;
if (image==="visible" || image===""){
toReturn.push([data[i], "visible"]);
}
}
window.location = 'Bus_confirmationSend';
//Where I used to do my request to write my data
/*DB::table('bus_line')->insert(
array('ID' => '', 'bus_direction' => '88t', 'card_uid' => '0xcb009299')
);*/
console.log(toReturn);
}
</script>
Here is what's inside web.php
Route::get('/bus_confirmationSend', 'bus_confirmationController#postRequest');
I know my connexion to my database is working as the function 'test' from the controller works, I can retrieve data from my DB. I also know that my first redirection to 'bus_directionController' works as the second redirection to 'home' works.
Am I missing something? I really don't understand what can be wrong here.
It may be because you have window.location = 'Bus_confirmationSend'; but your route starts with lower case b

How to update a record in lumen

I am simply trying to update a record but it says undefined variable in my view.
My html form view is like:
<form name="add_page" data-toggle="validator" role="form" id="add-pageform" action='{{url("update/page/$page->page_id")}}'>
My Controller:
public function updatePage(Request $request,$id)
{
$page = $request->all();
$plan = PageList::find($id);
$plan->update($page);
return view('edit-list');
}
and my route.php
$router->post('update/page/{id}','AjaxController#updatePage');
when i trying to access my view it says
Undefined variable: page (View: D:\xampp\htdocs\dev\app\resources\views\edit-list.blade.php)
its displaying error from in my form tag
any help would be appreciated:
You have not passed the $page variable to the view. Update your controller action like such:
public function updatePage(Request $request,$id)
{
$page = $request->all();
$plan = PageList::find($id);
$plan->update($page);
return view('edit-list')->with('page', $page);
}
For more information on passing data to views check this link
You should change your routes method to a patch if you want to update and then insert this into your form #method{'PATCH'}

Getting Error Of Undefined Variable "data" php Codeigniter

I am updating table taking id in url which is auto increment.. i am also defiend a variable data in controller that is $data['data']=$this->articlesmodel->find_data($art_id); and this varible passing to view that is $this->load->view('admin/edit',$data);
and in view at form open i called the variable id**}",['class'=>'form-horizontal']);?>
but the error occure that the $data is undefiend.
this is view code
<?php echo form_open("admin/update_data/{$data->id}",['class'=>'form-horizontal']);?>
This is controller code
public function edit_data($art_id)
{
$this->load->helper('form');
$this->load->model('articlesmodel');
$data['data']=$this->articlesmodel->find_data($art_id);
$this->load->view('admin/edit',$data);
}
This is Model Code
public function update_data($art_id, Array $data )
{
return $this->db
->where('id',$art_id)
->update('data',$data);
}

New route or another way to call a function from controller with ajax? Laravel 5.2 , Ajax

Is it possible to call a function in the controller without using a route or should I make a new route with two parameters as below that redirects to the specific page after the session has been added?
route::get('addsesion/{session-name}/{session slug};
If it's possible with ajax, can someone please point me in the right direction?
Basically what I would like to do is call the function addSession($session_name, $slug) from a controller with ajax on link <a href/> click , where it stores my specific session name and current page's slug.
It should call this addSession function on a click, store session data and then redirect to a different url. e.g. /seeparts, where it displays all saved session data.
Do I have to make a new route route::get('addsesion/{param1 - session-name}/{param2 - session slug}', currentController#addSession ); and then use that route as an ajax url? Or is there any other way how to use the controller's function?
My current Controller:
public function showAll() {
$parts = \DB::table() - > all();
$data = [
'parts' => $parts,
];
return view('partlist', $data);
}
public function showCpu($slug) {
// Specification query
$specs = \DB::table() - > select($select_columns) - > where('slug', $slug) - > first();
$data = [
'specs' => $specs,
'slug' => $slug
];
return view('part', $data);
}
//Add session - call this function
public function addSession($session_name, $slug) {\
Session::put($session_name, $slug);
}
}
part.blade.php:
<html>
#include('head.blade.php')
</body>
//on .add-to-partlist click adds session name that is specified in html and the current slug of the page
<a class="add-to-partlist" href="/seeparts" >Add to partlist</a>
</body>
</html>
I think you can use Service Injection binding controller function in your view.
Maybe you can reference it, https://laravel.com/docs/master/blade#service-injection.
For example:
<html>
#include('head.blade.php')
#inject('currentController', 'App\Http\Controllers\currentController')
</body>
//on .add-to-partlist click adds session name that is specified in html and the current slug of the page
<a class="add-to-partlist" href="/seeparts" onClick="{{ $currentController->addSession($session_name, $slug) }}">HERE</a>
</body>
</html>

Categories