when i load the page normally everything is going, the form is submitted and i get the right responses .. but when i load the page with .load() function jQuery the form doesnt submitted! and i get nothing from Ajax function!! any help
this is my HTML :
<div class="container">
<div class="row">
<h4>Manage Contents</h4>
<div class="table-responsive table3">
<table class="table table-bordered">
<thead>
<tr>
<th>#</th>
<th>Content</th>
<th>Owner</th>
<th>Created Date</th>
<th>Project</th>
<th>Priorty</th>
<th>Options</th>
</tr>
</thead>
<tbody>
#if ($availabe >= 1 )
<?php $i =1; ?>
#foreach ($avashown as $key)
<?php //this is edit content form?>
#if(isset($index) && $index == $i && isset($editid) && isset($idexist) && $idexist == $key->id)
<tr>
<form id="edit-content-form" action="/testajax" method="POST">
<td>{{ $i }}</td>
<td hidden="hidden">{{ csrf_field() }}</td>
<td hidden="hidden"><input type="hidden" name="id" value="{{ $editid }}"></td>
<td hidden="hidden"><input type="hidden" name="index" value="{{ $index }}"></td>
<td colspan="3">
<div class="control-group" id="inputField1">
<input class="form-control" type="text" name="content" value="{{ $key->content }}">
<span class="help-block hideme" id="help-block1"></span>
</div>
</td>
<td colspan="1">
<div class="control-group" id="inputField2">
<select class="form-control" name="project">
#foreach ($projects as $project)
<option value="{{ $project->id }}">{{ $project->projectname }}</option>
#endforeach
</select>
<span class="help-block hideme" id="help-block2"></span>
</div>
</td>
<td colspan="1">
<div class="control-group" id="inputField3">
<select class="form-control" name="priority">
#foreach ($priorities as $priority)
<option value="{{ $priority->id }}">{{ $priority->value }}</option>
#endforeach
</select>
<span class="help-block hideme" id="help-block3"></span>
</div>
</td>
<td>
<input type="submit" id="submitForm" name="submit" value="Done">
</td>
</form>
</tr>
<?php $i++;//i have to increase it here again?>
#else
<tr>
<td class="hidden"></td>
<td>{{ $i }}</td>
<td>{{ $key->content }}</td>
<?php $firstname = \App\User::find($key->owner_id);?>
<td>{{ ucfirst($firstname->firstname) }}</td>
<td>{{ $key->created_at }}</td>
<?php $projectname = \App\Project::find($key->project_id);?>
<td>{{ $projectname-> projectname }}</td>
<?php $priorty = \App\Priority::find($key->priority_id);?>
<td><span class ="label label-{{ $priorty->name }}">{{ $priorty->value }}<span></td>
<td>
<span class="glyphicon glyphicon-trash" data-toggle="tooltip" data-placement="bottom" title="Delete" onclick="ContentStatus('delete/ajax/{{ $key->id }}')"></span><span> </span>
<span id="editContentGlyph" data-toggle="tooltip" data-placement="bottom" title="Edit" class="glyphicon glyphicon-cog" onclick="editContent({{ $i }},{{ $key->id }})"></span>
</td>
<?php $i++; ?>
</tr>
#endif
#endforeach
#else
<tr>
<td>0</td>
<td colspan="6">Let's Work and Add some Contents! Create</td>
</tr>
#endif
</tbody>
</table>
</div>
</div>
</div>`
this is my script file :
function editContent(index,id){
var place = ".table3 tbody tr:nth-child("+index+") " ;
var place = "#alltables" ;
var des = place+" > *";
$(document).ready(function(){
$( place ).load("http://localhost:8000/manage_contents/"+index+"/"+id+" "+des, function(){
});
});
}
$(document).ready(function(){
$(function(){
$('#edit-content-form').on('submit',function(e){
$.ajaxSetup({
header:$('meta[name="_token"]').attr('content')
})
e.preventDefault(e);
$.ajax({
type:"PUT",
url:'http://localhost:8000/testajax',
data:$(this).serialize(),
dataType: 'json',
success: function(data){
$("#alerts").html(data.responseText);
if (data.updated) {
var place = ".table3 tbody tr:nth-child("+data.index+") " ;
var des = place+" > *";
$( place ).load("http://localhost:8000/manage_contents/ "+des, function(){
});
}
},
error: function(data,xhr, ajaxOptions, thrownError){
$.each(data.responseJSON, function(){
var i = 1;
$.each(this, function(index,error) {
$("#inputField"+i).addClass(" has-error");
$("#help-block"+i).removeClass(" hideme");
$("#help-block"+i).html("<strong>"+error+"</strong>");
i++;
});
});
},
})
});
});
});
`
I suspect its happening due to your HTML form is getting loaded via ajax and before that only your $(document).ready( code is getting executed. Hence at that time there will be no element with id edit-content-form present in DOM. So to deal with this situation try using event delegation technique like below:
$(function(){
$(document).on('submit','#edit-content-form',function(e){
// your existing code goes here
});
});
We are attaching the event listener to the document object which will be there for sure and then delegating to #edit-content-form whenever event occurs.
Related
In the group table I select a person from the list, but after selecting one name, his name should not appear in other select options, it must show 'selected' or just disappear, so names in groups cant duplicate, 1name=1slot.
In the students list below, there must be a group's name assigned to a person if he/she is selected.
View for a better understanding
My groups dropdown blade:
<div class="row">
#for ($groupName = 1; $groupName <= $project->amount; $groupName++)
<div class="col-3">
<table class="table table-bordered table-sm table-hover text-center my-3">
<tr class="table-light">
<th> Group #{{ $groupName }} </th>
</tr>
<tr>
<td>
#for ($j = 0; $j < $project->studentsPerGroup; $j++)
<select class="form-select form-select-sm" style="overflow:auto; max-height:260;">
<option selected>Assign Student</option>
#foreach ($students as $student)
<option value="{{ $groupName }}">{{ ucwords($student->name) }} {{ $loop->iteration . '/' . $loop->count }}</option>
#endforeach
</select>
#endfor
</td>
</tr>
</table>
</div>
#endfor
</div>
And persons list table:
enter code her<table class="table table-bordered table-sm table-hover text-center my-3">
<tr class="table-light">
<th>Student</th>
<th>Group Name</th>
<th width="220px">Action</th>
</tr>
#foreach ($students as $student)
<tr class="align-middle">
<td class="text-left">{{ ucwords($student->name) }}</td>
<td>Group#</td>
<td>
<form action="{{ route('students.destroy', $student->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" title="delete" class="btn btn-danger btn-sm" onclick="return confirm('Please Confirm')">
Remove
</button>
</form>
</td>
</tr>
#endforeach
</table>
It's a school project and I really tried hard. Any help is appreciated.
I want to check the first checkbox that have the id checkAll and it will check all my checkboxes and when I unchecked it, it will unchecked all too.
My blade index.blade.php:
<body>
<table class="table">
<thead>
<tr>
<th scope="col">
<input type="checkbox" id="checkAll">
</th>
<th scope="col">id</th>
<th scope="col">title</th>
<th scope="col">Paragraph</th>
</tr>
</thead>
#foreach ($categories as $categorie)
<tbody>
<tr>
<td>
<input type="checkbox" name="checkOne" value="{{$categorie->id}}">
</td>
<th scope="row">{{ $categorie->id }}</th>
<td>{{ $categorie->Title }}</td>
<td>{{ $categorie->Paragraph }}</td>
<form action={{ route('categorie.destroy', $categorie->id) }} method="POST">
#csrf
#method('DELETE')
<td>
<button class="btn btn-danger" type="submit">
delete
</button>
</form>
<button class="btn btn-warning"> <a href={{ route('edit', $categorie->id) }}>edit</a> </button>
</td>
</tr>
</tbody>
#endforeach
</table>
</body>
If you have multiple checkboxes with the same name then use array in your loop, as below:
<input type="checkbox" name="checkOne[]" value="{{ $categorie->id }}">
Now you need to use jQuery to select/unselect checkboxes, use below code:
$('#checkAll').change(function() { // main checkbox id
$('input[name="checkOne[]"]').prop('checked', $(this).is(":checked"));
});
Try this code -
<script type='text/javascript'>
function checkAll(e) {
const checkboxes = document.getElementsByName('checkOne');
for (let i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = e.checked;
}
}
</script>
#extends('layouts.app')
#section('content')
<body>
<button class="btn" onclick="deleteSelected()">delete</button>
<table class="table">
<thead>
<tr>
<th scope="col">
<input type="checkbox" id="all" onclick="chkd()">
</th>
<th scope="col">id</th>
<th scope="col">title</th>
<th scope="col">Paragraph</th>
</tr>
</thead>
#foreach ($categories as $categorie)
<tbody>
<tr>
<td>
<input type="checkbox" class="ss" value="{{ $categorie->id }}" onclick="deleteSelected()">
</td>
<th scope="row">{{ $categorie->id }}</th>
<td>{{ $categorie->Title }}</td>
<td>{{ $categorie->Paragraph }}</td>
<form action={{ route('categorie.destroy', $categorie->id) }} method="POST">
#csrf
#method('DELETE')
<td>
<button id="deleteRow" class="btn btn-danger" type="submit" style="display: none">
delete
</button>
</form>
<button class="btn btn-warning"> <a href={{ route('edit', $categorie->id) }}>edit</a> </button>
</td>
</tr>
</tbody>
#endforeach
</table>
</body>
#endsection
that is my view , when I check one of my checkbox and press the button delete I want this row to be deleted or when I checked all I want to delete all row
I think html input must inside the form. How about moving code like this:
#foreach ($categories as $categorie)
<tbody>
<tr>
<td>
<form action={{ route('categorie.destroy', $categorie->id) }} method="POST">
#csrf
#method('DELETE')
<input type="checkbox" class="ss" value="{{ $categorie->id }}" onclick="deleteSelected()">
<button id="deleteRow" class="btn btn-danger" type="submit" style="display: none">
delete
</button>
</form>
</td>
<th scope="row">{{ $categorie->id }}</th>
<td>{{ $categorie->Title }}</td>
<td>{{ $categorie->Paragraph }}</td>
<td><button class="btn btn-warning"> <a href={{ route('edit', $categorie->id) }}>edit</a> </button></td>
</tr>
</tbody>
#endforeach
But, I don't know this is can fix your case or not. I can't see the whole code
I have a table with its data are retrieved from database here: https://imgur.com/Sv4Suo7 . My problem is, I want to delete the data that are selected in the checkboxes.
I have tried putting name="ids[]" in my checkbox, but the data is still not sent to my controller. I have read somewhere that I need to use Javascript, but I don't know how to.
Views:
<div class="box-header with-border">
<div class="box-header-tools pull-left" >
<a href="{{ url('tasks/create/')}}" class="tips text-green" title="{{ Lang::get('core.btn_create') }} ">
<i class="fa fa-plus-square-o fa-2x"></i></a>
<a href="{{ url('tasks/massDelete')}}" onclick="" class="tips text-red" title="{{ Lang::get('core.btn_remove') }}">
<i class="fa fa-trash-o fa-2x delete_all" data-toggle="confirmation" data-title="{{Lang::get('core.rusure')}}" data-content="{{ Lang::get('core.rusuredelete') }}" ></i></a>
</div>
</div>
<div class="box-body" >
<div class="table-responsive" style="min-height:300px; padding-bottom:60px; border: none !important">
<table class="table table-striped table-bordered " id="{{ $pageModule }}Table">
<thead>
<tr>
<th align="center" class="number"> No </th>
<th align="center"> <input type="checkbox" class="checkall" id="master" /></th>
<th align="center">Task</th>
<th align="center">Due Date</th>
<th align="center">Assigned To</th>
<th align="center">Assigned By</th>
<th align="center">Status</th>
<th align="center">{{ Lang::get('core.btn_action') }}</th>
</tr>
</thead>
<tbody> #foreach($tasks as $task)
<tr>
<td width="30"> {{ ++$i }} </td>
<td width="50"><input type="checkbox" class="checkbox" name="ids[]" value="{{$task->id}}" /></td>
<td>{{$task->task_name}} </td>
<td>{{$task->due_date}}</td>
#foreach($users as $user)
#if($user->id == $task->assigned_id)<td>{{$user->username}}</td>#endif
#endforeach
#foreach($users as $user)
#if($user->id == $task->assigner_id)<td>{{$user->username}}</td>#endif
#endforeach
#if($task->status == 0)<td width="90">
<span class="label label-block label-info label-sm">Ongoing</span>
</td>#endif
#if($task->status == 1)<td width="90">
<span class="label label-block label-danger label-sm">Cancelled</span>
</td>#endif
#if($task->status == 2)<td width="90">
<span class="label label-block label-success label-sm">Completed</span>
</td>#endif
<td>
#if($task->status == 0)
{!! Form::open(array('url'=>'tasks/completeStatus/'.$task->id, 'class'=>'form-horizontal')) !!}
<button type="submit" name="markcomplete" class="btn" ><i class="fa fa-check-circle-o fa-2x"></i></button>
{!! Form::close() !!}
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
<input type="hidden" name="md" value="" />
</div>
</div>
</div>
</div>
Controller:
public function massDelete(Request $request)
{
$gotids = $request->input('ids');
if($gotids){
foreach($gotids as $id){
$task = Tasks::findOrFail($id);
$task->delete();
}
}
return redirect('tasks');
}
Route:
Route::get('/tasks/massDelete/', 'TasksController#massDelete');
I wanted the data to be in controller, when I tried dd($gotids); it displays null. Hope anyone can help.
Here is the code if you want to use javascript
Route::delete('delete-multiple-category', ['as'=>'category.multiple-delete','uses'=>'CategoryController#deleteMultiple']);
public function deleteMultiple(Request $request){
$ids = $request->ids;
Category::whereIn('id',explode(",",$ids))->delete();
return response()->json(['status'=>true,'message'=>"Category deleted successfully."]);
}
blade file
<div class="container">
<h3>PHP Laravel 5.6 - How to delete multiple row with checkbox using Ajax? - HDTuto.com</h3>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<button style="margin: 5px;" class="btn btn-danger btn-xs delete-all" data-url="">Delete All</button>
<table class="table table-bordered">
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>S.No.</th>
<th>Category Name</th>
<th>Category Details</th>
<th width="100px">Action</th>
</tr>
#if($categories->count())
#foreach($categories as $key => $category)
<tr id="tr_{{$category->id}}">
<td><input type="checkbox" class="checkbox" data-id="{{$category->id}}"></td>
<td>{{ ++$key }}</td>
<td>{{ $category->category_name }}</td>
<td>{{ $category->category_details }}</td>
<td>
{!! Form::open(['method' => 'DELETE','route' => ['category.destroy', $category->id],'style'=>'display:inline']) !!}
{!! Form::button('Delete', ['class' => 'btn btn-danger btn-xs','data-toggle'=>'confirmation','data-placement'=>'left']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#endif
</table>
<script type="text/javascript">
$(document).ready(function () {
$('#check_all').on('click', function(e) {
if($(this).is(':checked',true))
{
$(".checkbox").prop('checked', true);
} else {
$(".checkbox").prop('checked',false);
}
});
$('.checkbox').on('click',function(){
if($('.checkbox:checked').length == $('.checkbox').length){
$('#check_all').prop('checked',true);
}else{
$('#check_all').prop('checked',false);
}
});
$('.delete-all').on('click', function(e) {
var idsArr = [];
$(".checkbox:checked").each(function() {
idsArr.push($(this).attr('data-id'));
});
if(idsArr.length <=0)
{
alert("Please select atleast one record to delete.");
} else {
if(confirm("Are you sure, you want to delete the selected categories?")){
var strIds = idsArr.join(",");
$.ajax({
url: "{{ route('category.multiple-delete') }}",
type: 'DELETE',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data: 'ids='+strIds,
success: function (data) {
if (data['status']==true) {
$(".checkbox:checked").each(function() {
$(this).parents("tr").remove();
});
alert(data['message']);
} else {
alert('Whoops Something went wrong!!');
}
},
error: function (data) {
alert(data.responseText);
}
});
}
}
});
$('[data-toggle=confirmation]').confirmation({
rootSelector: '[data-toggle=confirmation]',
onConfirm: function (event, element) {
element.closest('form').submit();
}
});
});
</script>
I am trying to get the data of current log in user but i am getting the result of all users in database. While in all views same problem is accure please help.
My code is given
My view is personal.blade.php
#extends('layouts.app')
#section('content')
<div class="container"><br>
<h1 class="text-success text-center">Profile</h1><br>
<table class="table table-bordered">
#foreach($data as $value)
<tr>
<th>Name</th>
<td>{{ $value ->tname}}</td>
</tr>
<tr>
<td>Father Name</td>
<td>{{ $value ->fname}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $value ->email}}</td>
</tr>
<tr>
<td>Official Email</td>
<td>{{ $value ->off_email}}</td>
</tr>
#endforeach
</table>
<div class="form-group">
<label>Generate Report</label>
<div class="radio form-control">
<label class="col-md-4"><input type="radio" name="status" value="conf" >Personal</label>
<label class="col-md-4"><input type="radio" name="status" value="general">Publications</label>
</div>
</div>
<button class="btn btn-info" data-toggle="collapse" data-target="#demo"> Generate PDF</button>
<button class="btn btn-info" data-toggle="collapse" data-target="#demo"> Generate CSV</button>
<button class="btn btn primary">Go to Home</button><br>
<div id="demo" class="collapse">
<h3 class="text-success text-center">Complete your publication's detail</h3><br>
<div class="col-md-offset-3 col-md-6 m-auto d-block">
<form action="pub" method="post">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div>
#endsection
My controller is EducationController.php and its code is given.
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use App\education;
use DB;
class EducationController extends Controller
{
public function show()
{
//
$data['data'] = DB::table('users')->get();
if(count ($data)>0){
return view('personal',$data);
}
else
{
return view('personal');
}
}
}
My route code is given here.
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/personal','EducationController#personal');
Please help me. I dont know how to use the current user in all views. I am login while in result I am getting the result of all users which is stored in database. thanks in advance.
You use a #foreach which implies you expect multiple users in your view.
If you only want to show details for the logged in user you should do:
<?php
class EducationController extends Controller
{
public function show()
{
if(\Auth::check()){
$user = \Auth::user();
return view('personal',compact('user'));
}
else
{
return view('personal');
}
}
}
And in your view:
#extends('layouts.app')
#section('content')
<div class="container"><br>
<h1 class="text-success text-center">Profile</h1><br>
<table class="table table-bordered">
#if (isset($user))
<tr>
<th>Name</th>
<td>{{ $user->tname}}</td>
</tr>
<tr>
<td>Father Name</td>
<td>{{ $user->fname}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $user->email}}</td>
</tr>
<tr>
<td>Official Email</td>
<td>{{ $user->off_email}}</td>
</tr>
#endif
</table>
// Rest of view
It is because your code is actually for getting all users. You should do it like this :
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use App\education;
use DB;
class EducationController extends Controller
{
public function show()
{
$user = Auth::user();
return view('personal',compact('user'));
}
}
}
then in your view:
<tr>
<th>Name</th>
<td>{{ $user->tname}}</td>
</tr>
<tr>
<td>Father Name</td>
<td>{{ $user->fname}}</td>
</tr>
<tr>
<td>Email</td>
<td>{{ $user->email}}</td>
</tr>
<tr>
<td>Official Email</td>
<td>{{ $user->off_email}}</td>
</tr>
Note that if you have multiple auth, you must specify your guard :
Auth::guard('yourguard')->user();
otherwise, your user will return a null.