only first row is getting deleted while the other data is not - php

When I click on the delete modal of first row, the modal pops up and the data is getting deleted. If I click on the row other than the first row, it shows the error as id undefined.
Controller:
public function project_show(){
$this->load->view('project_show',$data);//page where delete button is present
$this->load->view('project_del');//modal page which opens on delete button
}
public function delete_get_id($pcode){
if($this->input->post('deleteproj'))
{
$this->project_model->delete_project($pcode);
}
}
ajax:
$('#deletebutton').click(function(){
var pcode = $(this).data('id');
$('#deleteproject').data('id', pcode);
});
$('#deleteproject').click(function(){
var pcode = $(this).data('id');
$('#deletemodal').modal('hide');
$('body').removeClass('modal-open');
$('.modal-backdrop').remove();
console.log(pcode);
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>" + "index.php/project/delete_get_id/"+ pcode,
data: {
pcode: pcode,
deleteproj: 1,
},
success: function (data) {
$("#deletemodal").modal('hide');
showproject();
}
});
});
view page of button:
<button id="deletebutton" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#deletemodal" data-id="<?php echo $row->project_code;?>"><span class = "fa fa-trash-o"></span> Delete</button>
view page of modal:
<div class="modal fade bs-example-modal-sm" id="deletemodal" ...>
.....
<button class="btn btn-danger btn delete" id="deleteproject"><span class="glyphicon glyphicon-trash"></span>Delete</button>
How will I pass the id along with "data-target" tag from page where button is present to modal page so that the particular row gets deleted

Please note that ID selector is supposed to be unique in a HTML document. And jQuery expects that to be true. So instead of using ID, you should use class name for the delete button
$('.deletebutton').click(function(){
var pcode = $(this).data('id');
$('#deleteproject').data('id', pcode);
});
//...
<button
class="deletebutton btn btn-danger btn-xs"
data-toggle="modal"
data-target="#deletemodal"
data-id="<?php echo $row->project_code;?>"
>
<span class = "fa fa-trash-o"></span> Delete
</button>
Assuming your showproject() function won't accidentally remove / recreate your #deleteproject button, things should work for you.

Related

send ID with href to php page with jquery

So, I have a CRUD and one button to modify that should redirect to another page with the id of the person, I tried to do it with jquery this way
fetchList()
function fetchList() {
$.ajax({
url: 'lista.php',
type: 'GET',
success: function (response) {
let task = JSON.parse(response);
let template = '';
task.forEach(task => {
template +=
`<tr pacienteId="${task.id_pac}">
<td>${task.apellido} </td>
<td>${task.nombre}</td>
<td>${task.dni}</td>
<td>${task.fecha_nacimiento}</td>
<td>${task.fecha_ingreso}</td>
<td>${task.obra_social}</td
// <td <span class="badge pacActivo">Activo</span> </td>
<td>
<div class="btn-group btn-group-toggle d-flex justify-content-center">
<i class="fas fa-edit"></i>
<button class="btn btn-sm butDel darBaja">
<i class="fas fa-trash-alt"></i>
</button>
</div>
</td>
</tr>`
});
$("#listadoPacientes").html(template);
}
});
}
and this is the modify
$(document).on('click', '#modificar', function (e) {
var href = $(this).attr('href');
let element = $(this)[0].parentElement.parentElement.parentElement
let id = $(element).attr('pacienteId')
// Save information
// Check if any ID is aviable
if (id) {
// Save the url and perform a page load
var direccion = href + '&id=' + id;
window.open(direccion);
console.log(id)
} else {
// Error handling
alert('algo salio mal')
}
})
but the problem it that I get this error:
Undefined index: id in
C:\xampp\htdocs\sistema\modulos\Pacientes\modificar.php
where i have the ID from jquery inside a variable
Solution:
You append your href attribute in jQuery with only an "&". You need an "?" instead of an "&".
Solution: You put an GET parameter behinde your jQuery HTML build process href-tag.
Short you are generating an URL like:
modificar.php&id=0
correct is
modificar.php**?**id=0
Example:
if (id) {
// Save the url and perform a page load
var direccion = href + '?id=' + id; // changed the & to an ?
window.open(direccion);
console.log(id)
} else {
// Error handling
alert('algo salio mal')
}

I want to update the product quantity in the basket

To update the quantity of products in the shopping cart
I use buttons. But I'm doing something wrong. Can you help?
sepet.blade.php CODE
<a href="#" class="btn btn-outline-dark btn-xs azalt"
data-id="{{ $UrunCartItem->rowId }}"
data-adet="{{$UrunCartItem->qty-1}}">-</a>
<span style="padding: 4px 12px">
{{ $UrunCartItem->qty }}
</span>
<a href="#" class="btn btn-outline-dark btn-xs artir"
data-id="{{ $UrunCartItem->rowId }}"
data-adet="{{$UrunCartItem->qty+1}}">+</a>
Javascript CODE
<script>
$(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.artir , .azalt').on('click' , function () {
debugger;
var id = $(this).attr('data-id');
var adet = $(this).attr('data-adet');
$.ajax({
type:'PATCH',
url:'/sepet/guncelle/' + id ,
data : { adet: adet } ,
success: function () {
window.location.href='/sepet';
}}); }); });
</script>
Page All Code
http://notes.io/8kex
At first, add event.preventDefault() under $('.artir , .azalt').on('click' , function () {.
Of course, you have to pass event like $('.artir , .azalt').on('click' , function (event) {)
Please, Share your Laravel code - maybe there is something not working or you have bug inside routes
PS: You don't need to pass new quantity inside the data-adet, but you can pass e.g. data-operation="plus" and data-operation="minus".

PHP AJAX button click counter

i want to make a button click counter. When you click the button the text from it changes, when the button is clicked i want to write to the database but i can't manage this.
<button type="button" class="btn btn-info" style="width:90%;" id="textChanger" onclick="counter;"><h4><i class="glyphicon glyphicon-earphone" aria-hidden="true"> </i> XXXX XXX XXX <h6>Show Number</h6></h4></button>
document.getElementById("textChanger").onclick= function(){
document.getElementById("textChanger").innerHTML="<h4><i class=\"glyphicon glyphicon-earphone\" aria-hidden=\"true\"> </i> <?php echo $nrTelefonAnunt ?> </h4>";
}
this is the code that i manage the text change, now what can i do to write to the database, i tried some methods but none worked
Thanks everybody for the answers i manged by adding this:
$('#textChanger').click(function(){
$.ajax({
url : 'rate.php?idanunt="<?php echo $idAnunt ?>"',
type : 'post',
success : function(data){
}
});
});
Try this
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<button type="button" class="btn btn-info" style="width:90%;" id="textChanger" onclick="counter;">1</button>
<script>
document.getElementById("textChanger").onclick= function(){
var count = document.getElementById("textChanger").innerHTML;
count = parseInt(count) + 1;
document.getElementById("textChanger").innerHTML=count;
}
</script>
Try this
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<button type="button" class="btn btn-info" style="width:90%;" id="textChanger" onclick="counter;"><h4><i class="glyphicon glyphicon-earphone" aria-hidden="true"> </i> <span>XXXXX</span> <h6>Show Number</h6></h4></button>
<script>
count=0;
$(document).on("click","button",function(){
count++;
$('span').text(count);
});
</script>

How to pass variable content into modal

I need to pass variable to modal
The variable is
$row['content']
My modal
<button class="open-AddBookDialog btn btn-default" data-id='.$row['content'].' data-toggle="modal" >Edit</button>
Script
<script>
$(document).on("click", ".open-AddBookDialog", function () {
var myBookId = $(this).data('id');
$(".modal-body #feature").val( myBookId );
alert(myBookId);
$('#myModal').modal('show');
});
</script>
I am getting the first words, No words shown which comes after space
Any experts??
Just make little change: ( add " double quote before and after '.$row['content'].' as below: )
<button class="open-AddBookDialog btn btn-default"
data-id="'.$row['content'].'" data-toggle="modal" >
Edit
</button>

CodeIgniter ajax post only working once

Hello I'm trying to delete a row in the database using ajax post but the problem is that the row gets deleted only once per page load. The row get detach from the HTML DOM everything I press the delete button, but the database only deletes one record. I want to delete therecord in the database every time I press the delete button. How can I achieve this ?
My view
<tbody id="tbody"><?php
$count = 1;
foreach($rows as $row):?>
<tr>
<td><?=$count?></td>
<td><a href="basic_table.html#">
<?=$row->FirstName." ".$row->LastName?></a>
</td>
<td class="hidden-phone"><?=$row->Email?></td>
<td><?=$row->Password?></td>
<td><span class="label label-warning label-mini">
<?=date("m/d/Y", strtotime($row->Created))?></span>
</td>
<td>
<button class="btn btn-success btn-xs">
<i class="fa fa-check"></i></button>
<button class="btn btn-primary btn-xs">
<i class="fa fa-pencil"></i>
</button>
<button id="delete" data-id="<?=$row->id?>"
data-url="<?=base_url()?>" class="btn btn-danger
btn-xs"><i class="fa fa-trash-o "></i>
</button>
</td>
</tr><?php
$count++;
endforeach?>
</tbody>
script
$(document).ready(function()
{
$("#tbody").on('click','#delete', function()
{
var id = $("#delete").data("id");
var url = $("#delete").data("url");
$.post(url + "users/delete", { id : id, cache: false } , function()
{
},"json");
$(this).closest('tr').detach();
});
});
Please give the codeigniter controller script
You must change to anchor tag and add preventDefault() to prevent reloading page
$(document).ready(function(){
$("#tbody").on('click','#delete', function(e){
//You must add e.preventDefault() to avoid reloading page
e.preventDefault();
var id = $("#delete").data("id");
var url = $("#delete").data("url");
$.post(url + "users/delete", { id : id, cache: false } , function(){
},"json");
$(this).closest('tr').detach();
});
});

Categories