I'm trying to delete multi rows from table using checkbox with Angular and php ,
but i'm not able to get that to work and my knowledge with Angular stoped here !
this is my table :
<div class="table-responsive" style="overflow-x: unset;">
<button class="btn btn-danger" ng-click="deleteBulk()" >Delete</button>
<table datatable="ng" dt-options="vm.dtOptions" class="table table-bordered table-striped" >
<thead>
<tr>
<th ><input type="checkbox" ng-model="master" > select</th>
<th>date</th>
<th>first name</th>
<th>last name</th>
<th>age</th>
<th>action</th>
<th>action</th>
<th>action</th>
</tr>
</thead>
<tbody>
<tr id="<?php echo $row["id"]; ?>" ng-repeat="name in namesData " >
<td name="checkbox">
<input type="checkbox" name="arrExample"
ng-model="arrInput" ng-true-value="{{name.id}}"
ng-checked="master"
ng-click='pushInArray(name.id)'>
</td>
<td>{{name.date}}</td>
<td>{{name.first_name}}</td>
<td>{{name.last_name}}</td>
<td>{{name.age}}</td>
<td><button type="button" ng-click="fetchSingleData(name.id)" class="btn btn-warning btn-xs">edit</button></td>
<td><button type="button" ng-click="deleteData(name.id)" class="btn btn-danger btn-xs">delete</button></td>
<td><button type="button" ng-click="single_move(name.id)" class="btn btn-success btn-xs">send</button></td>
</tr>
</tbody>
</table>
and this is my Angular code :
$scope.exampleArray = [];
$scope.pushInArray = function(id) {
// get the input value
var inputVal = id;
var array = $scope.exampleArray.push(inputVal);
$scope.deleteBulk = function(){
if(confirm("Are you sure you want to delete this record?")){
$http({
method:"POST",
url:"insert.php",
data:{'id':$scope.exampleArray, 'action' : 'deleteBulk'}
}).success(function(data){
$scope.success = true;
$scope.error = false;
$scope.successMessage = data.message;
$scope.fetchData();
});
}
};
};
and this is my php code inside insert.php
if($form_data->action == "deleteBulk")
{
$query = "
DELETE FROM orders WHERE id='".$form_data->id."'
";
$statement = $connect->prepare($query);
if($statement->execute())
{
$output['message'] = 'done!';
}
}
can anyone tell me what i'm doing wrong please ?
thank you
You are deleting one row each time with a unique id. So code base is correct.
To delete multiple rows, use the checkbox in each row and user will select multiple rows to delete and then delete rows by multiple ids.
You will also need to update SQL query to delete multiple rows at a time.
Something like below
DELETE FROM orders WHERE id IN (1, 2, 3, 4, ..., 10);
Related
I am a beginner at laravel,I have two form form1 which contains year and month and radio buttons of 1st and 2nd quanziane and selects of Salaries and chantiers and button when I click on button cherche for form1 to hide and form2 to display and also I get data according to the conditions of the form1 fields I use ajax this data I want to display it in form 2 table,for explain more I have Salaries working on Chantiers and who must point the day work every day, table Pointages contain id, Chantier_id foreign key of table Chantiers where they work, ouvrage_id (foreign key of table ouvrages) task gives to perform, datep date of Pointages, Sold price of Salaries for day, now I want to display in the table of balde payer.balde.php The total salaire of the worker who owes it to the company in which he works is per month or the 1st somaine of the month or the 2nd somaine of the month, depending on the client's choice, I could have data in the console but when I want to display it in my blade I get this error someone can resolve this error.
payer.balde.php
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th><input type="checkbox" id="check_all"></th>
<th>Anne</th>
<th>mois</th>
<th>matricule </th>
<th>prenom</th>
<th>salaire Net</th>
<th>sold</th>
<th>edit</th>
<th>delete</th>
<th>details</th>
</tr>
</thead>
<tbody>
#foreach($dataP as $salarie)
<tr id="{{$salarie->id}}">
<td><input type="checkbox" class="checkbox" name="customer_id[]" value="{{$salarie->id}}" /></td>
<td><input type="hidden" class='year' class="form-control" /></td>
<td><input type="hidden" class='month' class="form-control" /></td>
<td>{{ $salarie->id }} </td>
<td>{{ $salarie->nom }}</td>
<td>{{ $salarie->prenom }}</td>
<td>{{ $salarie->sold }}</td>
<td>Editer</td>
<form action="{{ url('salaries', ['id' => $salarie->id]) }}" method="post">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<td><button type="submit" class="btn btn-theme04 btn-sm" role="button">Delete</button></td>
</form>
<td>Show</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th></th>
<th>annee</th>
<th>mois</th>
<th>matricule </th>
<th>nom</th>
<th>prevom</th>
<th>sold</th>
<th></th>
<th></th>
<th></th>
</tr>
</tfoot>
</table>
</div>
</div>
<!------------->
</section>
</section>
<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);}
});
$("#form2").hide();
let now = new Date();
let year = now.getFullYear();
let month = ("0" + (now.getMonth() + 1)).slice(-2);
$('#annee').val(year);
$('#mois').val(month);
$("#hide").click(function(){
$("#form1").hide();
$("#form2").show();
let chan = $("#chantier_id option:selected").text();
$('#ch').text(chan);
let sal = $("#salarie_id option:selected").text();
$('#sa').text(sal);
let ann = $("#annee").val();
$('#an').text(ann);
let moi = $("#mois").val();
$('#mo').text(moi);
$('.year').text(ann);
let chan_id = $("#chantier_id option:selected").val();
let sal_id = $("#salarie_id option:selected").val();
$('.month').val(mois).parent().prepend(moi);
$('.year').val(ann).parent().prepend(ann);
var radioValue = $("input[name='quanzaime']:checked").val();
if(radioValue){
console.log(radioValue);
}
$.ajax({
type: 'get',
url: "{{ route('salarie.payerP') }}",
data: {
'quanzaime':radioValue,
'annee':ann,
'mois':moi,
'salarie_id':sal_id,
'chantier_id':chan_id
},
success: function(data1){
console.log(data1);
},
error: function(){
console.log('error');
},
});
});
});
</script>
SalarieController.php
public function payer(){
$pointages = Pointage::get();
$salaries = Salarie::get();
$chantiers = Chantier::get();
return view('salarie.payer', compact('salaries','pointages','chantiers'));
}
public function payerP(request $request){
if($request->quanzaime == 1){
$dataP = DB::table('salaries')
->join('pointages','pointages.salarie_id','salaries.id')
->selectRaw('SUM(pointages.sold) as sold,salaries.nom,salaries.prenom,salaries.id')
->whereRaw(DB::raw('YEAR(pointages.datep) = ' .$request->annee))
->whereRaw(DB::raw('MONTH(pointages.datep) = ' .$request->mois))
->whereRaw( DB::raw('DAY(pointages.datep)>0 AND DAY(pointages.datep)<16'))
->Orwhere('pointages.salarie_id','=',$request->salarie_id)->OrWhere('pointages.chantier_id','=',$request->chantier_id)
->groupBy('pointages.salarie_id')
->get();
return response()->json(['salarie'=>$dataP]);
}elseif($request->quanzaime == 2){
$dataP = DB::table('salaries')
->join('pointages','pointages.salarie_id','salaries.id')
->selectRaw('SUM(pointages.sold) as sold,salaries.nom,salaries.prenom,salaries.id')
->whereRaw(DB::raw('YEAR(pointages.datep) = ' .$request->annee))
->whereRaw(DB::raw('MONTH(pointages.datep) = ' .$request->mois))
->whereRaw( DB::raw('DAY(pointages.datep)>15 AND DAY(pointages.datep)<31'))
->Orwhere('pointages.salarie_id','=',$request->salarie_id)->OrWhere('pointages.chantier_id','=',$request->chantier_id)
->groupBy('pointages.salarie_id')
->get();
return response()->json(['salarie'=>$dataP]);
}else{
$dataP = DB::table('salaries')
->join('pointages','pointages.salarie_id','salaries.id')
->selectRaw('SUM(pointages.sold) as sold,salaries.nom,salaries.prenom,salaries.id')
->whereRaw(DB::raw('YEAR(pointages.datep) = ' .$request->annee))
->whereRaw(DB::raw('MONTH(pointages.datep) = ' .$request->mois))
->Orwhere('pointages.salarie_id','=',$request->salarie_id)->OrWhere('pointages.chantier_id','=',$request->chantier_id)
->groupBy('pointages.salarie_id')
->get();
return response()->json(['salarie'=>$dataP]);
}
//dd($request);
}
web.php
Route::get('/payer','SalarieController#payer');
Route::get('/payerP','SalarieController#payerP')->name('salarie.payerP');
Hi I'm trying to get the id for each row shown in while loop in my jquery function
Here are the request and view (PHP code)
<div class="container">
<table class="table table-striped">
<div class="table responsive">
<thead class="black white-text">
<tr>
<th>#</th>
<th>Nom et Prénoms</th>
<th>Fonction</th>
<th>Université</th>
<th>Email</th>
<th>Contact</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$result = $connection->query($stmt) ;
if($result->rowCount() > 0)
{
while($row = $result->fetch(PDO::FETCH_BOTH))
{
echo '<tr>
<td scope="row">'.$row["id"].'</td>
<td>'.$row["nom"].'</td>
<td> '.$row["fonction"].'</td>
<td> '.$row["nomU"].'</td>
<td> '.$row["email"].'</td>
<td> '.$row["Contact"].'</td>
<td>' ?>
<button type="button" class="btn btn-success valider">Valider</button> /<button type="button" class="btn btn-danger rejeter">Rejeter</button>
<?php
echo '</td>
<td>'
?>
<input type="hidden" class="the_id" value='<?php echo $row[' id ']; ?>'>
<?php echo '</td>
</tr>';
}
}
else
{
echo "0 results";
}
?>
</tbody>
</div>
</table>
</div>
And I want to show the id of each row when I click on the button "valider" for the line with jquery but it's showing the first id for all the row.
Here is th jquery code
$(document).ready(function(){
$('.valider').click(function(e){
alert($('.the_id').val());
});
});
Almost right. Remember that when accessing by a class name rather than an ID, you will get all of those objects.
So, we need to be more specific. Give this a try:
$('.valider').click(function(e){
var id = $(this).parent().parent().find('td').find('.the_id').val();
alert(id);
});
This takes you from $(this), your button, to its parent <td>, then the parent <tr>, then looks for any classes of .the_id inside the tr, in other words, the specific one you want.
I usually add a custom attribute to the elements, like data-custom-id.
'<button type="button" class="btn btn-success valider" data-custom-id="'.$row["id"].'">Valider</button>'
And then in the calling function I can access the object with this
$('.valider').click(function(e){
alert($(this).attr('data-custom-id'));
});
You need to go up to the parent tr to get the related input so you could use closest() with the $(this) :
$('.valider').click(function(e){
alert( $(this).closest('tr').find('.the_id').val() );
});
Following is my form:
<form method="POST" action="../controller/assignsubteacher.php">
<table class="table table-bordered table-striped table-hover table-condensed" id="coursedetail" >
<thead>
<tr>
<th>Sub Id</th>
<th>Sub Name</th>
<th>Teacher Name</th>
</tr>
</thead>
<tbody id="table_ajax">
</tbody>
<tfoot>
<tr>
<th>Sub Id</th>
<th>Sub Name</th>
<th>Teacher Name</th>
</tr>
</tfoot>
</table>
<div class="col-md-2">
<button type="submit" class="btn btn-primary btn-block btn-flat">Submit</button>
</div>
</form>
and table in the form is populated by following response:
while($row=mysqli_fetch_array($result))
{
$list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
<option value = "UNKNOWN" selected="select">-SELECT-</option>';
$get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
$teacher_list = mysqli_query($con,$get_teacher);
while($row_Teacher=mysqli_fetch_array($teacher_list))
{
$list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
}
$list.='</select>';
$Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
//$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
$Subject_Name=$row["Subject_Name"];
$tr.='<tr>
<td>'.$Subject_ID.'</td>
<td>'.$Subject_Name.'</td>
<td>'.$list.'</td>
</tr>';
$COUNT=$COUNT+1;
}
echo $tr;
I am not able to use the posted data to insert in to database. Is there any way i can send the data as Array and retrieve it.
following is the AJAX to populate the table body:
xhr.onreadystatechange = function()
{
if (this.readyState == 4 && this.status == 200)
{
console.log(xhr.responseText);
Table.innerHTML=xhr.responseText;
}
};
I was thinking of using foreach to insert data in the POST controller, but have no idea how to achieve that.
any help would be appreciated.
Use SQL insert query to do so.
while($row=mysqli_fetch_array($result))
{
$list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
<option value = "UNKNOWN" selected="select">-SELECT-</option>';
$get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
$teacher_list = mysqli_query($con,$get_teacher);
while($row_Teacher=mysqli_fetch_array($teacher_list))
{
$list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
}
$list.='</select>';
$Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
//$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
$Subject_Name=$row["Subject_Name"];
$tr.='<tr>
<td>'.$Subject_ID.'</td>
<td>'.$Subject_Name.'</td>
<td>'.$list.'</td>
</tr>';
$COUNT=$COUNT+1;
$sql = "INSERT INTO `table` (column1, column2, column3) VALUES ($list, $subject_name, $tr)";
//Replace columns & table name
if(mysqli_query($con, $sql)) {
echo "Inserted";
}
}
echo $tr;
I'm trying to make simple delete ajax function. I'm not very good ad JS at all but when I click on button delete I can see in Chrome Dev Console that id = NULL and when I refresh the page the entry is back. The ajax function is pretty simple
$("body").on("click",".remove-item",function(){
var entry_id = $(this).parent("td").data('entry_id');
var c_obj = $(this).parents("tr");
$.ajax({
dataType: 'json',
type:'POST',
url: 'delete.php',
data:{entry_id:entry_id}
}).done(function(data){
c_obj.remove();
toastr.success('Item Deleted Successfully.', 'Success Alert', {timeOut: 5000});
});
});
And the button
<button class="btn btn-danger remove-item" > Delete</button>
And this is the delete.php
$entry_id = $_POST["entry_id"];
$stmt = $pdo->prepare("DELETE FROM entries WHERE entry_id = :entry_id");
$stmt->bindParam(':entry_id', $entry_id, PDO::PARAM_INT);
$stmt->execute();
echo json_encode([$entry_id]);
Any idea what can be wrong?
update. The table
echo '<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Transaction Quote</th>
<th>Action</th>
</tr>
</thead>';
foreach($pdo->query("SELECT * FROM entries ORDER BY entry_id DESC LIMIT 20") as $row){
echo '
<tbody>
<tr>
<td>'.$row['entry_id'].'</td>
<td>'.$row['entry_name'].'</td>
<td>'.$row['entry_transaction_quote'].'</td>
<td><button class="btn btn-sm btn-danger remove-item" ><i class="glyphicon glyphicon-trash"></i> Delete</button></td>
</tr>
</tbody> ';
}
echo ' </table>
Change your td with class name
<td class="entry_id">'.$row['entry_id'].'</td>
jquery
var entry_id = $(this).parent("td").parent("tr").find('.entry_id').text().trim();
Try this
Html should be like
<tr entry_id="<?=$row['entry_id']?>">
<td><button class="btn btn-sm btn-danger remove-item" ><i class="glyphicon glyphicon-trash"></i> Delete</button></td>
</tr>
Js code should be like
var entry_id = $(this).parent().parent().attr("entry_id");
I have construted my table as follows:
<table id="dataTable">
<thead>
<tr><th>Name</th>
<th>Value</th></tr>
</thead>
<TR><TD>Scooby Doo</TD><TD>6</TD><TD><INPUT TYPE="Button" onClick="AddRow()" VALUE="Add Row"></TD></TR>
</table>
When the button Add Row is clicked, I need to change the button to a delete button and insert a new row on the first line. The first line must contain the same as in the code. How can I do this?
On clicking the delete button, Imust be able to delete the row to which the delete button belong?
Hope this helps
$(function(){
$("input[type='button'].AddRow").toggle(
function(){
var el = $(this);
el.closest('tr').clone(true).prependTo(el.closest('table'));
el.attr("value", "Delete row");
},
function(){
$(this).closest('tr').remove();
});
});
<table id="dataTable" border="1">
<thead>
<tr>
<th>
Name
</th>
<th>
Value
</th>
</tr>
</thead>
<tr>
<td>
Scooby Doo
</td>
<td>
6
</td>
<td>
<input type="Button" value="Add Row" class="AddRow">
</td>
</tr>
</table>
Working Demo
Something like?
$('#dataTable thead').prepend('<tr><th>Name</th><th>Value</th></tr>');
And for the deleting:
$('#dataTable thead tr').click(function() {
$(this).hide(); //hide the row, this doesn't delete it.
});
.