laravel 4 download file from different folders - php

Hi Guys i just need some help on my download page in my project because i need to have a download page that get files from different folders all of the folder is in the public path do you have some ideas for this i am using a page just like the link below just ignore the other button.
Download Page
i just tried ajax for this but it doesn't work
this my view:
#include('partials.navbar')
<link rel="stylesheet" type="text/css" href="http://localhost:8000/assets/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="http://localhost:8000/assets/css/search.css">
<!-- Search -->
<div class="container">
<!-- Search -->
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="input-group" id="adv-search">
<input type="text" class="form-control" placeholder="Search file" />
<div class="input-group-btn">
<div class="btn-group" role="group">
<div class="dropdown dropdown-lg">
<button type="button" class="set-width btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><span class="caret"></span></button>
<div class="dropdown-menu dropdown-menu-right" role="menu">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="file">File type</label>
<select class="form-control">
<option value="pf">Public Weather Forecast</option>
<option value="sf">24 Shipping Forecast</option>
<option value="gale">Gale Warning Forecast</option>
<option value="advisory">Weather Advisory</option>
<option value="tca">Tropical Cyclone Advisory</option>
<option value="swb">Severe Weather Bulletin</option>
<option value="iws">International Warning for shipping</option>
<option value="wof">Weather Outlook Forecast</option>
<option value="spf">Special Forecast</option>
<option value="sm">Surface Maps</option>
</select>
</div>
<div class="form-group">
<label for="date">Date</label>
<input class="form-control" type="date" />
</div>
<div class="form-group">
<label for="file">File name</label>
<input class="form-control" type="text" />
</div>
<button type="submit" class="btn btn-primary"><i class="fa fa-search"></i></button>
</form>
</div>
</div>
<button type="button" class="btn btn-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!--- Datatable -->
<div class="container">
<div class="row">
<div class="col-md-12">
<h4>Downloads</h4>
<table id="mytable" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>File Name</th>
<th>Date Issued</th>
<th>File ID Number</th>
<th>Uploader</th>
<th>Action</th>
</tr>
</thead>
<tfoot>
<tr>
<th>File Name</th>
<th>Date Issued</th>
<th>File ID Number</th>
<th>Uploader</th>
<th>Action</th>
</tr>
</tfoot>
<tbody>
#foreach ($files as $files2)
<tr>
<td>{{ $files2->file_name }}</td>
<td>{{ $files2->date }}</td>
<td>{{ $files2->id }}</td>
<td>{{ $files2->username }}</td>
<td><a data-id="{{ $files2->id }}" href="/download" class="btn btn-primary btn-xs dload-button" ><i class="fa fa-download"></i></a>
<button data-id="" class="btn btn-primary btn-xs dload-button" data-title="Dload" data-toggle="modal" data-target="#dload-modal"><i class="fa fa-file-text"></i></button></td>
</tr>
#endforeach
</tbody>
</table>
<input type="hidden" name="id" value="">
<input type="hidden" name="type" value="">
<input type="hidden" name="filename" value="">
</div>
</div>
</div>
</div>
#include('partials.footer')
<script type="text/javascript" src="http://localhost:8000/assets/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="http://localhost:8000/assets/js/dropdown.js"></script>
<script type="text/javascript" src="http://localhost:8000/assets/js/datatable.js"></script>
<script>
$(function() {
$(".dload-button").click(function(){
var param = $(this).data('id');
$.ajax({
url: "/downloadfile/" + param,
success: function(msg){
var dload = JSON.parse(msg)[0];
console.log(dload)
$('#id').val(dload.id);
$('#type').val(dload.file_type);
$('#filename').val(dload.upload);
},
error:function(){
alert("failure");
}
});
});
});
my controller:
public function dloadFile($id)
{
$files = Files::where('id',$id)
->get();
return json_encode($files);
}
public function getDownload()
{
$id = Input::get('id');
$files = Files::where('id',$id)
->first();
$ftype = $files->file_type = Input::get('type');
$filename = $files->upload = Input::file('filename');
$file= public_path(). "uploads/{$ftype}";
$headers = array(
'Content-Type: => application/pdf',
);
return Response::download($file, '{$filename}', $headers);
}
my route:
Route::get('/downloadfile/{id}', 'FileController#dloadFile');
Route::get('/download', array('uses' => 'FileController#getDownload'));
Any idea is much more appreciated Thanks in advance!.

there is no need to use Ajax for download. there is my code to download product image.in view file there is download link.
<a data-id="{{ $product->id }}" href="/productCRUD/{{$product->id}}/download" class="btn btn-primary btn-xs dload-button" ><i class="fa fa-download">Download</i></a>
in routes.php
Route::get('productCRUD/{product}/download', 'ProductCRUDController#download');
in productCRUDController.php
public function download($id)
{
$files = Product::where('id',$id)
->first();
$ftype = $files->file_type = Input::get('type');
$fullPath= public_path(). "/uploads/{$files->filePath}";
$headers = array(
'Content-Type: => application/jpg',
);
return Response::download($fullPath,$files->filePath, $headers);
}
with this code when you click on download link you get the file in your downloads folder.

Related

Call a modal with data ajax laravel 8

I have a problem with my app , when i click the edit btn in the table then my modal is showed with data and when i click the second row in the table then my modal is not show.
this my controller :
public function edit_kpi_program_plan(Request $request)
{
$data = kpi_input_program::where('id' , $request->id)->first();
return response()->json($data, 200);
}
this my route :
Route::post('/edit-program-input', [InputController::class, 'edit_kpi_program_plan'])->name('edit.program');
this my button in blade:
Edit
this my modal in blade:
<!-- Modal Input Program Plan Value Update -->
<div class="modal fade" id="new_modal_plan" tabIndex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Input Program Plan</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="{{ route('update-kpi-program-plan') }}" method="post">
#csrf
<div class="form-group">
<label for="exampleFormControlSelect1">Pilih Item</label>
<select name="id_program_plan_update" class="form-control" id="exampleFormControlSelect1">
#forelse ($data_drop_update as $item)
<option value="{{ $item->id }}">{{ $item->program }}</option>
#empty
<td colspan="18"><h3 class="text-center">No Data Inputed</h3></td>
#endforelse
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Konfirmasi Pilihan</label>
<select name="kpi_program_plan_id" class="form-control" id="exampleFormControlSelect1">
#forelse ($data_drop as $item)
<option value="{{ $item->id }}">{{ $item->program }}</option>
#empty
<td colspan="18"><h3 class="text-center">No Data Inputed</h3></td>
#endforelse
</select>
</div>
<div class="form-group">
<table class="table table-bordered">
<thead>
<th>Jan</th>
<th>Feb</th>
<th>Mar</th>
<th>Apr</th>
<th>May</th>
<th>Jun</th>
<th>Jul</th>
<th>Aug</th>
<th>Sep</th>
<th>Oct</th>
<th>Nov</th>
<th>Dec</th>
</thead>
<tbody>
#foreach ($dataf as $item)
<td><input type="text" name="jan_plan" id="jan_plan" style="max-width: 40px"></td>
<td><input type="text" name="feb_plan" id="feb_plan" style="max-width: 40px"></td>
<td><input type="text" name="mar_plan" id="mar_plan" style="max-width: 40px"></td>
<td><input type="text" name="apr_plan" id="apr_plan" style="max-width: 40px"></td>
<td><input type="text" name="may_plan" id="may_plan" style="max-width: 40px"></td>
<td><input type="text" name="jun_plan" id="jun_plan" style="max-width: 40px"></td>
<td><input type="text" name="jul_plan" id="jul_plan" style="max-width: 40px"></td>
<td><input type="text" name="aug_plan" id="aug_plan" style="max-width: 40px"></td>
<td><input type="text" name="sep_plan" id="sep_plan" style="max-width: 40px"></td>
<td><input type="text" name="oct_plan" id="oct_plan" style="max-width: 40px"></td>
<td><input type="text" name="nov_plan" id="nov_plan" style="max-width: 40px"></td>
<td><input type="text" name="dec_plan" id="dec_plan" style="max-width: 40px"></td>
#endforeach
</tbody>
</table>
</div>
<button class="btn btn-primary" type="submit" id="btn-save">Save</button>
</form>
</div>
</div>
</div>
</div>
and this my ajax :
<script>
$(document).ready(function($) {
$('#btn-edit-plan').on('click', function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var id = $(this).data('id');
console.log(id);
// ajax
$.ajax({
type: "POST",
url: "{{ url('edit-program-input') }}",
data: { id: id },
dataType: 'json',
success: function(res) {
$('#new_modal_plan').modal('show');
$('#jan_plan').val(res.jan);
$('#feb_plan').val(res.feb);
$('#mar_plan').val(res.mar);
$('#apr_plan').val(res.apr);
$('#may_plan').val(res.may);
$('#jun_plan').val(res.jun);
$('#jul_plan').val(res.jul);
$('#aug_plan').val(res.aug);
$('#sep_plan').val(res.sep);
$('#oct_plan').val(res.oct);
$('#nov_plan').val(res.nov);
$('#dec_plan').val(res.dec);
}
});
});
})
</script>
The id attribute should be unique for every element in the page. but it is not like this with class attribute.
so update your button:
Edit
and update your jQuery selector to:
$(document).on('click', '.btn-edit-plan', function() {
// Your code here
})
You can learn more about it here: Difference between class and id in jQuery

How can I display dynamically in my select option my students with first name and last name

I'm beginner in Laravel. I'm using ajax. I want to display data of students (eleves) in my select option by first name (prénom) last name (nom), but the problem that it displays only the first name (prénom) without the last name (nom). However I need to associate first name and last name. Please help me.
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Note;
use App\Matiere;
use App\Eleve;
use App\Classe;
use DB;
class DropdownlistController extends Controller
{
public function index()
{
$notes = Note::all();
$matieres = Matiere::all();
$eleves = Eleve::all();
$classes = Classe::all();
$classes = DB::table("classes")->pluck("classe","id");
return view('admin.dropdownlists',compact('classes','notes','matieres','eleves'));
}
public function store(Request $request)
{
Note::create($request->all());
return redirect()->back();
}
public function getStateList(Request $request)
{
$eleves = DB::table("eleves")
->where("classe_id",$request->classe_id)
->pluck("nom" ,"prenom","id");
return response()->json($eleves);
}
public function getCityList(Request $request)
{
$matieres = DB::table("matieres")
->where("classe_id",$request->classe_id)
->pluck("nom_matiere","id");
return response()->json($matieres);
}
}
My view there I'm using my function ajax
#extends('layouts.admin')
#section('content')
<section class="wrapper">
<div class="table-title">
<div class="row">
<div class="col-sm-6">
<h2> ESpace de Gestion <b></b> des notes </h2>
</div>
<div class="col-sm-6">
<a href="#addEmployeeModal" class=" btn btn-success" data-toggle="modal"><i class="material-icons"></i>
<span>Ajouter un nouvelle note </span></a>
</div>
</div>
</div>
<div class="row mt">
#if(session()->has('success'))
<div class="alert alert-success">
{{session()->get('success')}}
</div>
#endif
<div class="col-lg-12">
<div class="content-panel">
<section id="no-more-tables">
<table class="table table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>id-note</th>
<th>Nom</th>
<th>classe</th>
<th>matiére</th>
<th>Note</th>
<th>les actions</th>
</tr>
</thead>
<tbody>
#foreach($notes as $note)
<tr>
<td class="numeric" data-title="id-parent">{{$note->id}}</td>
<td class="numeric"
data-title="id-parent">{{$note->eleve->nom}} {{$note->eleve->prenom}}</td>
<td class="numeric" data-title="id-parent">{{$note->classe->classe}}</td>
<td class="numeric" data-title="id-parent">{{$note->matiere->nom_matiere}}</td>
<td class="numeric" data-title="Login">{{$note->note}}</td>
<td>
<button href="#editEmployeeModal" class="btn btn-theme"
data-target="#editEmployeeModal " data-catid={{$note->id}} class="edit"
data-toggle="modal"><i class="material-icons" data-toggle="tooltip"
title="Edit"></i></button>
<button href="#deleteEmployeeModal" class="btn btn-theme"
data-target="#deleteEmployeeModal" data-catid="{{$note->id}}"
class="delete" data-toggle="modal"><i class="material-icons"
data-toggle="tooltip"
title="Delete"></i>
</button>
</td>
</tr>
</tbody>
#endforeach
</table>
<div class="text-center">
</div>
<div class="clearfix">
<div class="hint-text">Affichage de <b>5</b> sur <b>25</b> entrées</div>
<div id="addEmployeeModal" href="create" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('dropdownlists.store')}}" method="post">
{{csrf_field()}}
<div class="modal-header">
<h4 class="modal-title">Ajouter un éléve</h4>
<button type="button" class="close" data-dismiss="modal"
aria-hidden="true">×
</button>
</div>
<div class="modal-body">
<div class="panel-body">
<div class="form-group">
<label for="title">Select classe:</label>
<select id="classe" name="classe" class="form-control"
style="width:350px">
<option value="" selected disabled>Select</option>
#foreach($classes as $key => $classe)
<option value="{{$key}}"> {{$classe}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="title">Select classe:</label>
<select id="eleve" name="eleve" class="form-control"
style="width:350px">
</select>
</div>
<div class="form-group">
<label for="title">Select matiere:</label>
<select id="matiere" name="matiere" class="form-control"
style="width:350px">
<option value="" selected disabled>Select</option>
</select>
</div>
<div class="form-group">
<label>la note </label>
<input type="text" id="note" name="note" class="form-control"
required>
</div>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal"
value="Annuler">
<input type="submit" class="btn btn-success" value="Ajouter">
</div>
</form>
</div>
</div>
</div>
</div>
#endsection
#section('js')
<script type="text/javascript">
$('#classe').change(function () {
var classeID = $(this).val();
if (classeID) {
$.ajax({
type: "GET",
url: "{{url('get-state-list')}}?classe_id=" + classeID,
success: function (res) {
if (res) {
$("#eleve").empty();
$("#eleve").append('<option>Select</option>');
$.each(res, function (key, value) {
$("#eleve").append('<option
value = "'+eleve.age+'" > '+value+' < /option>');
});
} else {
$("#eleve").empty();
}
}
});
} else {
$("#classe").empty();
$("#matiere").empty();
}
});
$('#classe').on('change', function () {
var classeID = $(this).val();
if (classeID) {
$.ajax({
type: "GET",
url: "{{url('get-city-list')}}?classe_id=" + classeID,
success: function (res) {
if (res) {
$("#matiere").empty();
$.each(res, function (key, value) {
$("#matiere").append('<option
value = "'+key+'" > '+value+' < /option>');
});
} else {
$("#matiere").empty();
}
}
});
} else {
$("#matiere").empty();
}
});
</script>
#endsection
Routes:
Route::resource('dropdownlists','DropdownlistController');
Route::get('get-state-list','DropdownController#getStateList');
Route::get('get-city-list','DropdownController#getCityList');
Laravel has a very powerful and very useful feature that will make this easy for you, mutators. By adding a 'fullName' mutator, you can do this in the model any time you want. Then, in your controllers, you can call the function as if it was a field.
So, on your Eleve model:
public function getFullNameAttribute(){
return $this->prenom. " " . $this->nom;
}
Then in any controller, like when you want to send these to a dropdown, you can just get your collection, and then transfer it into the array you need.
Get the collection:
$elevesCollection = \App\Eleve::orderBy('nom')->select('prenom', 'nom', 'id')->get();
Then, pull the full name and the id from the collection:
$eleves = $elevesCollection ->pluck('name', 'id')->toArray();
You can send that through to your form dropdown. Please double check my code / spelling, but this should do what you wish.

Laravel delete button with HTML form

I'm using the HTML form, not Laravel Collective.
For now I've successfully created a CRUD for a users in my CMS, but one thing bothers me:
How can I set a Delete button in my list of users, instead of the specific edit page?
Also, it will be nice when a user clicks on the Delete button to show up confirmation popup for deleting the specific user.
So, here's my code:
The controller:
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user = User::findOrFail($id);
$user->delete();
return redirect('/admin/users');
}
The list of users page:
#extends('layouts.backend')
#section('content')
<h1>Users</h1>
<a class="btn btn-primary" href="/admin/users/create">Create new user</a>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
<th>Operations</th>
</tr>
</thead>
<tbody>
#if($users)
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->role ? $user->role->name : 'User has no role'}}</td>
<td>{{$user->status == 1 ? 'Active' : 'Not active'}}</td>
<td>{{$user->created_at->diffForHumans()}}</td>
<td>{{$user->updated_at->diffForHumans()}}</td>
<td>
Edit
<a class="btn btn-danger" href="">Delete</a> // HOW TO ACHIEVE THIS?
</td>
</tr>
#endforeach
#endif
</tbody>
</table>
#endsection
The specific edit user page:
#extends('layouts.backend')
#section('content')
<h1>Edit user</h1>
<form method="POST" action="/admin/users/{{$user->id}}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label>Name:</label>
<input type="text" name="name" class="form-control" value="{{$user->name}}">
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" name="email" class="form-control" value="{{$user->email}}">
</div>
<div class="form-group">
<label>Role:</label>
<select name="role_id" class="form-control">
#if($user->role_id == 1)
<option value="1" selected>Administrator</option>
<option value="2">Editor</option>
#else
<option value="1">Administrator</option>
<option value="2" selected>Editor</option>
#endif
</select>
</div>
<div class="form-group">
<label>Status:</label>
<select name="status" class="form-control">
#if($user->status == 1)
<option value="1" selected>Active</option>
<option value="0">Not active</option>
#else
<option value="1">Active</option>
<option value="0" selected>Not active</option>
#endif
</select>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" value="{{$user->password}}">
</div>
<div class="form-group">
<input type="submit" name="submit" value="Update user" class="btn btn-primary">
</div>
</form>
<form id="delete-form" method="POST" action="/admin/users/{{$user->id}}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<input type="submit" class="btn btn-danger" value="Delete user">
</div>
</form>
#include('inc.errors')
#endsection
The route:
Route::group(['middleware'=>'admin'], function(){
Route::resource('admin/users', 'AdminUsersController');
Route::get('/admin', function(){
return view('admin.index');
});
// Route::resource('admin/posts', 'AdminPostsController');
});
It's not obvious from the code you posted, but your DELETE route expects DELETE method. As it should!
But on your list you're trying to access it with GET method.
Really you should just reuse the code from the edit page, which fakes DELETE method already.
Something like this:
...
<td>
Edit
<form method="POST" action="/admin/users/{{$user->id}}">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="form-group">
<input type="submit" class="btn btn-danger delete-user" value="Delete user">
</div>
</form>
</td>
...
...
// Mayank Pandeyz's solution for confirmation customized for this implementation
<script>
$('.delete-user').click(function(e){
e.preventDefault() // Don't post the form, unless confirmed
if (confirm('Are you sure?')) {
// Post the form
$(e.target).closest('form').submit() // Post the surrounding form
}
});
</script>
As you have stated it will be nice when a user clicks on the Delete button to show up confirmation popup for deleting the specific user. For this you have to use jquery and ajax. Change the following code:
<a class="btn btn-danger" href="">Delete</a>
to
<a class="btn btn-danger delete_user" href="javascript:void(0);" id="{{$user->id}}">Delete</a>
and put an event listener like:
$('.delete_user').click(function(){
if( confirm('Are you sure?') )
{
var id = $(this).attr('id');
// Make an ajax call to delete the record and pass the id to identify the record
}
});
You can update your code like:
<a class="btn btn-danger" href="/admin/users/{{$user->id}}/delete" >Delete</a>
OR you should delete user using route name like:
Delete
option with "laravel form helper" and jquery
<div class="actions">
<a href="#" class="list-icons-item delete-action">
<i class="icon-trash"></i>
</a>
{{ Form::open(['url' => route('admin.users.destroy', $user), 'method' => 'delete']) }}
{{ Form::close() }}
</div>
<script>
$(document).ready(function () {
$('.delete-action').click(function (e) {
if (confirm('Are you sure?')) {
$(this).siblings('form').submit();
}
return false;
});
});
</script>
Using route closures to delete
show.blade.php
<form>
<h1>Title: {{$tutorial->title}}</h1>
<p>Title Description: {{$tutorial->title_description}}</p>
<p>Video: {{$tutorial->video}}</p>
<form action="{{ route('delete-tutorial', [$tutorial->id])}}"
method="post">
#csrf
#method('DELETE')
<button class="btn btn-primary" onclick="return confirm('Are you sure?')"
type="submit" name="Delete">Delete</button>
</form>
for the route for deleting
Route::delete('tutorial/{id}',function($id){
$tutorial = Tutorial::findOrFail($id)->first();
$tutorial->delete();
return redirect('tutorial');
})->name('delete-tutorial');
Also don't forget to add this on your routes/web.php
use App\Models\Tutorial;

Saving multiple table rows at once (Laravel 4)

I have a table where I get the data from database than i need to update all rows at once. Inside each cell I have added input fields. Now i want to be able to update all users at once when I enter the data but I dont know how.
Below is a picture of my view:
#extends('admin/master')
#section('content')
<section class="content">
{{Form::open()}}
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
<div class="col-lg-4">
<h3 class="box-title">INPUT EXAM RESULTS FOR EACH STUDENT: </h3>
</div>
<div class="pull-right col-lg-8">
<div class="col-lg-2 col-lg-offset-5 pull-left">
<select class="btn bg-navy" name="city" >
<option>Select City</option>
<option>Prishtin</option>
<option>Prizren</option>
</select>
</div>
<div class="col-lg-5 pull-right">
<div class="input-group">
<input type="text" name="search_input" class="form-control" placeholder="Search here...">
<div class="input-group-btn">
<button type="submit" class="btn btn-info"><i class="fa fa-search"></i> Search</button>
</div>
</div>
</div>
</div>
</div>
<div class="box-body">
<?php if (isset($data)) { ?>
<table id="example2" class="table table-bordered table-hover">
<thead>
<tr>
<th>{{Lang::get('messages.stid')}} </th>
<th>{{Lang::get('messages.name')}}</th>
<th>Subject 1</th>
<th>Subject 2</th>
<th>Subject 3</th>
<th>Subject 4</th>
</tr>
</thead>
<tbody>#foreach ($data as $row)
<tr>
<td>{{$row->id}}</td>
<td>{{$row->fname}} {{$row->lname}}</td>
<td><input type="text" name="sub1" class="form-control" placeholder="Add marks here..."></td>
<td><input type="text" name="sub2" class="form-control" placeholder="Add marks here..."></td>
<td><input type="text" name="sub3" class="form-control" placeholder="Add marks here..."></td>
<td><input type="text" name="sub4" class="form-control" placeholder="Add marks here..."></td>
</tr>
#endforeach
</tbody>
</table>
<button type="submit" class="col-lg-2 pull-right btn btn-success"><i class="fa fa-save"></i> Save</button>
<?php } ?>
</div>
</div>
</div>
</div>
{{Form::close()}}
</section>
{{ HTML::script('/admin/assets/js/jquery-2.2.3.min.js') }}
{{ HTML::script('/admin/assets/js/bootstrap.min.js') }}
{{ HTML::script('/admin/assets/js/jquery.dataTables.min.js') }}
{{ HTML::script('/admin/assets/js/dataTables.bootstrap.min.js') }}
<script>
$(function () {
$("#example1").DataTable();
$('#example2').DataTable({
"paging": true,
"lengthChange": false,
"searching": false,
"ordering": true,
"info": true,
"autoWidth": false
});
});
</script>
#stop
//Controller
public function search_input(){
$input = Input::get('search_input');
$city = Input::get('city');
$data = Apply::where('exam_venue', '=', "$input")
->where('city_applied', '=', "$city")->get();
if (isset($_POST['save'])) {
}
return View::make('admin/exam/edit',compact('data'));
}
Your input elements need to be arrays, keyed by $row->id (I assume this is the primary key of your students table) to identify which student that row represents. For example:
<td><input type="text" name="sub1[{{ $row->id}}]" class="form-control" placeholder="Add marks here..."></td>
Then when you submit the data, look up the student by the array key and update their information accordingly. Something to the effect of:
$key = // extract id from sub1 array
Student::find($request->input($key));
// do updates here
// repeat for each sub input...

Submit INPUT to the database to return as a Table PHP jQuery MySQL

I'm trying to refresh a data input into an INPUT through a form and then SUBMIT this to the database to return it as a table on the same page. I want it to do this without showing the refresh of the page.
This is what I have currently. When I put data into any of the INPUT fields and click SUBMIT nothing happens and nothing shows for an error.
<?php
session_start();
error_reporting (E_ALL);
require '../core/cnn.php';
if(isset($_POST['submitsearchprojno'])) {
$searchprojno = $_POST["searchprojno"];
}
if(isset($_POST['submitsearchadd'])) {
$searchaddress = $_POST["searchaddress"];
}
if(isset($_POST['submitsearchpc'])) {
$searchpostcode = $_POST["searchpostcode"];
}
$searchpostcode = mysql_real_escape_string($searchpostcode);
$searchaddress = mysql_real_escape_string($searchaddress);
$searchprojno = mysql_real_escape_string($searchprojno);
?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h4 class="modal-title">Search Projects</h4>
</div>
<div class="modal-body">
<form class="modal-form" id="searchform" name="searchform" action="" method="post">
<div class="form-group">
<label class="col-md-4 control-label">Project Number</label>
<div class="col-md-6">
<input type="text" class="form-control input-inline input-medium" name="searchprojno" id="searchprojno" placeholder="Enter Project Number">
</div>
<button type="submit" class="btn blue" id="submitsearchprojno" name="submitsearchprojno" >Search <i class="m-icon-swapright m-icon-white"></i></button>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Address</label>
<div class="col-md-6">
<input type="text" class="form-control input-inline input-medium" name="searchaddress" id="searchaddress" placeholder="Enter Address">
</div>
<button type="submit" class="btn blue" id="submitsearchadd" name="submitsearchadd" >Search <i class="m-icon-swapright m-icon-white"></i></button>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Postcode</label>
<div class="col-md-6">
<input type="text" class="form-control input-inline input-medium" name="searchpostcode" id="searchpostcode" placeholder="Enter Postcode">
</div>
<button type="submit" class="btn blue" id="submitsearchpc" name="submitsearchpc" >Search <i class="m-icon-swapright m-icon-white"></i></button>
</div>
<div class="form-group">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-striped table-bordered table-advance table-hover">
<thead>
<tr>
<th class="col-md-9"><i class="fa fa-list-alt"></i> Address</th>
<th class="col-md-3"></th>
</tr>
</thead>
<tbody>
<tr>
<?php $searchrs = mysql_query("SELECT ProjectNo, CONCAT(COALESCE(HouseNoName, ''), ' ', COALESCE(StreetName, ''), ' ',
COALESCE(TownOrCity, ''), ' ', COALESCE(Postcode, '')) AS Display, PropID, AreaID, AWGMember, Householder, HouseNoName,
StreetName, TownOrCity, Postcode, ContactTelephone, AlternatePhone, Email, PropertyTenure, PropertyNotes
FROM prop_property
WHERE IsActive = 1
AND (Postcode = '".$searchpostcode."'
OR StreetName = '".$searchaddress."'
OR ProjectNo = '".$searchprojno."')
") or die(mysql_error());
$checkrs = mysql_query("SELECT * FROM prop_property WHERE IsActive = 0");
if(!mysql_num_rows($checkrs) > 0) {
echo '<td> No record found!</td><td></td>';
}
else {
while ($results = mysql_fetch_array($searchrs)) {
echo '
<td id="displayadd">'.$results['Display'].'</td>
<td>
<form action="../jobdetails.php" method="post">
<input type="hidden" name="searchhouse" value=" '.$results['HouseNoName'].'" >
<input type="hidden" name="searchstreet" value=" '.$results['StreetName'].'" >
<input type="hidden" name="searchtown" value=" '.$results['TownOrCity'].'" >
<input type="hidden" name="searchpostcode" value=" '.$results['Postcode'].'" >
<input type="hidden" name="searchpropid" value=" '.$results['PropID'].'" >
<input type="hidden" name="searchprojectno" value=" '.$results['ProjectNo'].'" >
<button type="submit" class="btn default btn-xs blue-stripe" id="viewsearch" name="viewsearch">View Address</button>
</form>
</td>';
}
}?>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</form>
<div class="modal-footer right">
<button type="button" data-dismiss="modal" class="btn default">Cancel</button>
</div>
<script type="text/javascript">
$(function(){
$('#searchform').on('submit', function(e){
e.preventDefault();
//alert($('#searchpostcode').val())
$.post('includes/jobdetailssearch.php',
$('#searchform').serialize(),
function(data, status){
$('.table-responsive #displayadd').html(data.Display);
//$("#table-responsive td").last().append(data);
console.log("done");
}).fail(function () {
console.log("fail");
});
});
});
</script>
How can I get it to POST the INPUT to the database and return in the table?

Categories