I am trying to upload a file. After file upload, the page should be visible with the previous data. What I have done:
I have two users agronomist and a grower. If I choose a grower, the grower profile should be visible, and upload the file in the grower profile, but after file upload, the selected grower data is not visible. how to make selected data visible after file upload. What I have tried is:
blade.php
<div >
<form >
<h1 >Account Information</h1>
<div >
<label for="growers_lsit">Growers List:</label>
<div >
<select id="growers_list" onchange="fetchGrower();">
<option value="">Select Growers</option>
@foreach($grower_data as $data)
<option value="{{$data->first_name}},{{$data->id}}">{{$data->first_name}} </option>
@endforeach
</select>
</div>
</div>
</form>
</div>
<div id="grower-data-div">
<div id="grower_form" >
<h1 >Grower Profile</h1>
<div >
<div >
<input type="text" name="beets" placeholder="Beets" id="grower_name" value="" readonly="">
</div>
<div >
<input type="text" name="beets" placeholder="Primary Crop" >
</div>
</div>
<div >
<div >
<h3>Recommendations </h3>
</div>
</div>
<form method="POST" action="/uploadRecommends" enctype="multipart/form-data">
{{ csrf_field() }}
<div >
<input type="hidden" name="grower_id" value="" id="grower_id">
<div >
<div >
<div >
<div >
<div >
<input type="file" name="file" id="inputGroupFile02">
<label for="inputGroupFile02">Choose file</label>
</div>
<div >
<label id=""> </label>
</div>
</div>
</div>
<div >
<div >
<input type="submit" name="upload" value="Upload PDF" id="upload" >
</div>
</div>
</div>
</div>
</div>
</form>
<div >
<div >
<table id="table" >
<thead>
<tr>
<th>PDF Name </th>
<th>Date</th>
<th>View</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
javascript file
<script>
function fetchGrower()
{
var e = document.getElementById("growers_list");
var option = e.options[e.selectedIndex];
var opArr=option.value.split(",");
var grower_id=opArr[1];
var grower_name=opArr[0];
$.ajax({
url: 'fetchPdf/' grower_id,
type: 'get',
dataType: 'json',
success: function(response){
$('#table tbody').empty();
$("#grower-data-div").css("display", "block");
$("#grower_name").val(grower_name);
$("#grower_id").val(grower_id);
var len=response['data'].length;
if(len > 0){
for(var i=0; i<len; i ){
var pdf_name = response['data'][i].pdf_name;
var pdf_date = response['data'][i].pdf_date;
var pdf_id = response['data'][i].id;
var tr_str = "<tr>"
"<td align='center' style='width:20vw;'>" pdf_name "</td>"
"<td align='center'>" pdf_date "</td>"
"<td align='center'><a href='/openPdf/" pdf_name "'><i class='fa fa-eye'></i></td>"
"<td align='center'><a href='/deleteFile/" pdf_id "'><i class='fa fa-remove'></i></td>"
"</tr>";
$("#table tbody").append(tr_str);
}
}
else{
var tr_str = "<tr>"
"<td align='center' colspan='4'>No record found.</td>"
"</tr>";
$("#table tbody").append(tr_str);
}
}
});
}
</script>
web.php
Route::post('/uploadRecommends', 'HomeController@uploadRecommends');
Route::get('/fetchPdf/{id}', 'HomeController@fetchPdf');
HomeController.php
public function uploadRecommends(Request $request)
{
$grower_id=$request->input('grower_id');
$agronomist_id=auth()->user()->id;
if(array_key_exists('file', $request->all()))
{
$pdf_file=$request->file;
$name=$pdf_file->getClientOriginalName();
$filename = pathinfo($name, PATHINFO_FILENAME);
$extension = $pdf_file->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $pdf_file->storeAs('public/cca_license/pdf_upload',$fileNameToStore);
DB::insert('insert into grower_recommends(agronomist_id,grower_id,pdf_name) values(?,?,?)',[$agronomist_id,$grower_id,$fileNameToStore]);
echo "<script>alert('File Uploaded Successfully');window.location.href='/agronomist_account';</script>";
}
}
public function fetchPdf($gro_id)
{
$res=DB::table('users')->where('id',auth()->user()->id)->get();
$first_name=$res[0]->first_name;
$last_name=$res[0]->last_name;
$name=$first_name." ".$last_name;
// $res1=DB::table('user_role')->where('id',auth()->user()->id)->get();
$agro_data= DB::table('user_roles')
->select('user_roles.roles_id','users.first_name','users.id')
->join('users','users.id','=','user_roles.user_id')
->where('user_roles.roles_id','=',2)
->get();
$pdf_data=DB::table('grower_recommends')->where('agronomist_id',auth()->user()->id)->where('grower_id',$gro_id)->latest('pdf_date')->get();
$userData['data'] = $pdf_data;
echo json_encode($userData);
exit;
//return view('grower_account')->with('pdf_data',$pdf_data)->with('name',$name)->with('agro_data',$agro_data);
}
When I select a user, the page shows data like this.
[![Output1][1]][1]
After uploading file, the page shows output as
[![Output2][2]][2]
How to show the user data , after file upload. [1]: https://i.stack.imgur.com/8z0RQ.jpg [2]: https://i.stack.imgur.com/Peg2K.png
CodePudding user response:
In controller( method fetchPdf()) return $pdF_data without assigning to any array.
CodePudding user response:
(1) /uploadRecommends this route call form the ajax function and then after you need to call the fetchGrower function in blade file
without page refresh...
(2) you can use Laravel Http session to use for store data of grower or agronomist in session using the session you can just redirect respected page or blade file.
CodePudding user response:
Try this
$request->flash();
return redirect()->back();
Or
session()->flashInput();
return redirect()->back();
Check Flashing inputs into session, laravel documentation Or Flashing request data into session for next requests