How to update table data onclick same button? - php

<script>
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
uid = $("input[name='user_id']:checked").map(function() {
return this.id;
}).get().join(",");
$.ajax({
type:"POST",
data:{"uid":uid},
url:"<?php echo base_url(); ?>hr/shortlist",
success:function(data){
setTimeout(function(){
location.reload();
}, 1000);
}
});
});
});
</script>
<table>
<thead>
<tr class="info">
<th>Check</th>
<th>Recruiter Id</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<input type="checkbox" name="user_id" id="uid1211120937" class="user_id">
</td>
<td>20181123091338</td>
</tr>
<tr>
<td>
<input type="checkbox" name="user_id" id="uid1211092847" class="user_id">
</td>
<td>20181123091338</td>
</tr>
</tbody>
</table>
<input type="submit" name="submit" id="submit" value="short"/>
shortlist.php
$uid = explode(",",$this->input->post('uid'));
foreach($uid as $user_id)
{
$data = array('shortlist'=>'1');
$this->db->where('uid',$user_id);
$query = $this->db->update('personal_detail',$data);
}
In this question, I have multiple checkboxes. Now, What I want when if I selected all checkbox then shortlist must be 1 for all will be updated and if I unchecked some checkbox then uncheck checkbox value will be 0. So, How can I do this? Please help me.
Thank You

You need to find all checkbox state respect to userid.
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
var uids = [];
$("input[name='user_id']").map(function() {
uids.push({id: this.id, value: this.checked ? 1 : 0});
});
$.ajax({
type:"POST",
data:{"uid":uids},
url:"<?php echo base_url(); ?>hr/shortlist",
success:function(data){
setTimeout(function(){
location.reload();
}, 1000);
}
});
});
});
Now in shortlist.php
$uid = $this->input->post('uid');
foreach($uid as $user)
{
$data = array('shortlist'=> $user['value']);
$this->db->where('uid',$user['id']);
$query = $this->db->update('personal_detail',$data);
}

Related

post input name array of while loop

there is array in while loop please check with code below
<?php
$addItemSql = "SELECT * from tbl_invoice_services where col_cust_id='$getID' ORDER BY col_service_id";
$addItemResult = mysqli_query($db, $addItemSql);
while ($addItemRow = mysqli_fetch_assoc($addItemResult)) {
?>
<tr>
<td>
<input type="hidden" id="col_service_product1" name="col_service_product1[]" value="<?php echo $addItemRow['col_service_product']; ?>">
</td>
</tr>
<?php} ?>
and want to post those array value to php
ajax code
$('.saveInvoice').on('click', function (ev) {
$.ajax({
url: "saveInvoce.php",
type: "POST",
data: {
col_service_product1:$('#col_service_product1').val()
},
success: function (data) { }
});
});
try this demo code
file1
<?php
$i=0;
while ($i<5) {
?>
<tr>
<td>
<input type="hidden" id="col_service_product1" name="col_service_product1" value="<?php echo $i; ?>">
</td>
</tr>
<?php $i++; } ?>
<button value="Save Invoice" class="saveInvoice">Save Invoice</button>
<script type="text/javascript" src="../library/q.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.saveInvoice').click(function () {
var arr=[];
var data=document.getElementsByName("col_service_product1");
for(var i=0;i<data.length;i++){
arr[i]=data[i].value;
}
$.ajax({
url: "saveInvoce.php",
type: "post",
data: {col_service_product1:arr},
success: function (data) {
alert(data);
}
});
});
});
</script>
file2
<?php
$data=$_POST['col_service_product1'];
//code for save data in table
foreach($data as $value){
echo $value; //query here
}

How to send multiple same name input fields value via ajax post method

I have two same name multiple input fields. I want to send all fields value from another page using jquery ajax post method but i am not getting all rows input fields value. Please review my code.
Javascript code
<script type="text/javascript">
function getValue()
{
$.post("paidamt.php",
{
paidamt : $('#paidamt').val(),
uid : $('#uid').val()
},
function( data){
/*alert(data);*/
$("#divShow").html(data);
});
}
</script>
Html Code
<div>
<form method="post">
<table border="1">
<tr>
<th>Product</th>
<th>Price</th>
<th>Paid Amount</th>
<th>Check</th>
</tr>
<?php
$sql = mysql_query("SELECT * FROM `tbldemo`");
while ($result = mysql_fetch_array($sql)) {
?>
<tr>
<td><?php echo $result['pname']; ?> </td>
<td><?php echo $result['price']; ?></td>
<td><input type="text" name="paidamt[]" id="paidamt"></td>
<td><input type="checkbox" name="uid[]" id="uid"
value="<?php echo $result['id']; ?>"></td>
</tr>
<?php }
?>
</table><br>
<input type="button" name="submit" id="submit"
onclick="getValue(1)" value="Save Amt.">
</form>
</div>
<div id="divShow">
</div>
Try this one
var paidamt = $("input[name=paidamt]").map(function(){
return $(this).val();
}).get().join(",");
var uid = $("input[name=uid]").map(function(){
return $(this).val();
}).get().join(",");
$.ajax(
{
type: "POST",
url: 'paidamt.php',
data:
{
paidamt:paidamt,
uid:uid
}
});
Firstly you have given the input elements the same id which is repeated in the loop. This will end up in your HTML being invalid, you should change the id to class:
<form method="post">
<table border="1">
<tr>
<th>Product</th>
<th>Price</th>
<th>Paid Amount</th>
<th>Check</th>
</tr>
<?php
$sql = mysql_query("SELECT * FROM `tbldemo`");
while ($result = mysql_fetch_array($sql)) { ?>
<tr>
<td><?php echo $result['pname']; ?> </td>
<td><?php echo $result['price']; ?></td>
<td><input type="text" name="paidamt[]" class="paidamt"></td>
<td><input type="checkbox" name="uid[]" class="uid" value="<?php echo $result['id']; ?>"></td>
</tr>
<?php }
?>
</table><br>
<button type="submit" name="submit" id="submit">Save Amt.</button>
</form>
To actually send the input values in the AJAX request you can simply serialize() the containing form when the form is submit:
$(function() {
$('form').submit(function(e) {
$.ajax({
url: "paidamt.php",
type: 'POST',
data: $(this).serialize(),
success: function(data) {
$("#divShow").html(data);
});
});
});
});
I suggest to add class instead of id, since identically class can be repeated but id should not.
<script type="text/javascript">
function getValue()
{
var paidamtval = [];
$('#paidamt').each(function(){
paidamtval.push($(this).val());
});
$.post("paidamt.php",
{
paidamt : paidamtval,
uid : $('#uid').val()
},
function( data){
/*alert(data);*/
$("#divShow").html(data);
});
}
</script>
Since you will have many of these, id - needs to be unique, which in your case isn't, so remove "id="paidamt"
<td><input type="text" name="paidamt[]" id="paidamt"></td>
That's your first mistake. And secondly don't use $.post, to submit this form. Either remove AJAX submit, or bind form using something like jQuery Form plugin.
You try this code
$('document').ready(function(){
$('#submit').click(function(){
jQuery.ajax({
type: "POST",
url: "paidamt.php",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(html){
try{
$("#divShow").html(data);
}catch (e){
alert(JSON.stringify(e));
}
},
error : function(e){alert("error "+JSON.stringify(e)); }
});
});
});
in you paidamt.php file
$paidamt=$_POST['paidamt'];// its can array values
print_r($paidamt);// result display

multiple select with checkbox in php

i am making a website in which i am to embbed the functionality of delete using multiple checkbox. here is my code. my problem is
1. Ajax call is not working.
2. how can i make search from database for array .
<?php
if(isset($_POST['Delete']))
{
$array=$_POST['check_box'];
}
?>
<form method="post" id="form">
<table width="200" border="1">
<tr>
<td>select</td>
<td>NAme</td>
<td>Action</td>
</tr>
<?php
while($selectnumberarr=mysql_fetch_array($selectnumber))
{
?>
<tr>
<td><input type="checkbox" name="check_box[]" class="check_box" id="<?php $selectnumberarr[0]; ?>" /> </td>
<td><?php echo $selectnumberarr[1]; ?></td>
</tr>
<?php
}?>
<input type="submit" name="Delete" id="delete">
</table>
</form>
and below is my ajax and javascript code.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#delete').click(function() {
$.ajax({
type: "POST",
url: "checkbox.php",
data: $('#form').serialize(),
cache: false,
success: function(html)
{
alert("true");
}
});//end ajax
});
});
</script>
any help would be appriciated
your code is almost correct. You need to remove `onChange="show()" for input checkbox, because if you have jquery then you don't need to put events on HTML elements.
Use jquery 'on' method for compatibility for latest php library.
Replace your jquery code with following jquery code :-
<script>
$(document).ready(function(){
$('#delete').on('click',function()
{
var cat_id = $('.check_box:checked').map(function() {
return this.id;
}).get().join(',');
console.log(cat_id);
$.ajax({
type: "POST",
url: "checkbox.php",
data: { "kw ":cat_id },
datatype:'json',
success: function(html)
{
alert("true");
}
});//end ajax
});
});
</script>
Use ".check_box" instead of "element" in jquery to prevent checks for all checkboxes, instead of desired ones.
Hope it helps.
Why you don't use an array for sending the checkboxes like:
HTML part:
<?php
if (isset($_POST['check_box'])) {
var_dump($_POST['check_box']);
echo "ajax call is working";
}
?>
<form id="form">
<table width="200" border="1">
<tr>
<td>select</td>
<td>NAme</td>
<td>Action</td>
</tr>
<?php
while ($selectnumberarr = mysql_fetch_array($selectnumber)) {
?>
<tr>
<td><input type="checkbox" name="check_box[]" class="check_box" value="<?php echo $selectnumberarr[0]; ?>" /> </td>
<td><?php echo $selectnumberarr[1]; ?></td>
</tr>
<?php
}
?>
</table>
<input type="button"name="delete" id="delete" value="Delete" />
</form>
JQuery part:
<script type="text/javascript">
$(document).ready(function(){
$('#delete').click(function() {
$.ajax({
type: "POST",
url: "checkbox.php",
data: $('#form').serialize(),
cache: false,
success: function(html)
{
alert("true");
}
});//end ajax
});
});
</script>
So you can easily get an array of the selected checkboxes in php with:
$idsArray = $_POST["check_box"];
this looks now like:
array(
"1", "2","etc.."
);
so this array contains all the ids of the selected checkboxes for delete.

How to delete all entry from table using checkBox clickall after confirmation is true? jQuery Ajax PHP

I am trying to delete all records from the table using the checkbox. When the user checked the topmost checkbox, it will check all other checkboxes inside the loop then a confirmation box will appear about deleting the records. if the user click OK, the the all the records will be deleted using $.ajax, if he clicked Cancel, then, the page will return to the same state, and the checkboxes are not checked anymore.
<?php
include 'dbconn.php';
?>
<table border="1" >
<tr><td align="center" width="20"><input type="checkbox" name='checkALL' id='checkALL'></td><td>Name</td>
</tr>
<?php
$sql=mysql_query("SELECT * FROM names ORDER BY names ASC") or die(mysql_error());
while($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><input type="checkbox" name="id[]" id="id[]" value="<?php print $rows['id'];?>">
</td><td>Name</td>
</tr>
<?php
}
?>
</table>
JQUERY
<script>
$(function(){
//click all
$('#checkALL').click(function(){
$(':checkbox').attr({checked: 'true'});
var del=confirm("You checked all the box. Delete All?");
if(del==true){
//delete here using $.ajax
}
else{
window.location.reload(false);
$('#checkAll').attr({checked: 'false'});
}
});
});
</script>
Place this in your if block.
$.ajax({
type: "GET",
url: '<php file which truncates table>',
success: function (data) {
if (data == 'truncated') {
alert('success');
} else {
alert('not truncated');
}
}
});
Return the string truncated from your php file on success.
Here U- PageUrl, A- Action on page, P- Parameter
if (confirm('Are you sure to delete this record?')) {
Operation(U, A, P);
}
function Operation(U, A, P) {
$.ajax({
type: "POST",
url: U + '/' + A,
data: P,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
cache: false,
success: function (r) {
var str = r.d;
}
});
}
You can use something like this in your HTML check all page
<html>
<head><title>Select/Delete ALL with jQuery/PHP</title>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<table border="1" cellpadding="5">
<tr>
<th>
<input type="checkbox" id="checkAll" />
</th>
</tr>
<tr>
<td><input type="checkbox" class="check" name="posts[]" value="100" /></td>
</tr>
<tr>
<td><input type="checkbox" class="check" name="posts[]" value="200" /></td>
</tr>
<tr>
<td><input type="checkbox" class="check" name="posts[]" value="300" /></td>
</tr>
</table>
<script>
$(document).ready(function(){
var checked = false;
$('#checkAll').click(function(){
if (checked == false){
checked = true
} else {
checked = false
}
$('input.check').attr("checked",checked);
var confirmDelete=confirm("You checked all the box. Delete All?");
if (confirmDelete==true){
var csv = '';
$('.check').each(function() {
csv += $(this).attr("value") + ",";
});
$.ajax({
type: "POST",
url: "delete.php",
data: { tobeDeleted: csv }
}).done(function( msg ) {
console.log( "Data has been deleted: " + msg );
});
} else {
$('#checkAll').attr("checked", false);
$('input.check').attr("checked", false);
}
});
});
</script>
</body>
</html>
and use something like this in your PHP script
<?php
$postDeleted = substr($_POST['tobeDeleted'], 0, strlen($_POST['tobeDeleted'])-1);
$arrDeleted = explode(",", $postDeleted);
$sql = "DELETE FROM employee WHERE 1=1 ";
foreach($arrDeleted as $key=>$value){
$sql .= "OR employee_id = $value ";
}
echo $sql;
?>
if(del==true){
$.ajax({
type: "POST",
url: 'your_file_url',
async:false,
cache: false,
success: function(data){
if (data == 1) {
alert('success');
} else {
alert('Error');
}
}
});
}
In your php file, echo 1 for successful delete and 0 for some error

Adding a table row after insert with jQuery $.ajax PHP/MySQL

I need a help to adding a table row after inserting with PHP/MySQL. I don't know what else to do.
FYI i'm able to insert with $.ajax and my select are working correctly. I need a help on how to retrieve this information to the table row.
agenda.php
<form class="form-inline" id="form-agenda">
<input type="text" name="data" class="input-big datepicker" placeholder="Data">
<input type="text" name="local" class="input-big" placeholder="Local">
<input type="text" name="cidade" class="input-big" placeholder="Cidade">
<button type="submit" name="submit" class="btn btn-primary">Gravar</button>
</form>
<table class="table table-hover">
<thead>
<tr>
<th>Data</th>
<th>Local</th>
<th>Cidade</th>
</tr>
</thead>
<tbody id="teste">
<?php
$agendaDAO = new AgendaDAO();
foreach($agendaDAO->select() as $row){
echo '<tr>'.
'<td>'.$row['data'].'</td>'.
'<td>'.utf8_encode($row['local']).'</td>'.
'<td>'.$row['cidade'].'</td>'.
'<td><i class=icon-edit></i> <i class=icon-remove></td>'.
'</tr>';
}
?>
</tbody>
</table>
agenda.js
$('#form-agenda').submit(function(){
var fields = $(this).serialize();
$.ajax({
url: 'ajax-agenda.php',
type: 'POST',
data: fields,
dataType: 'text',
beforeSend: function(){
$('#loading-indicator').css({display: 'block'});
},
complete: function(){
$('#loading-indicator').css({display: 'none'});
},
success: function(data) {
$('#teste').prepend(data);
$('#teste tr:first').slideDown('slow');
}
});
$(this).trigger('reset');
return false;
});
ajax-agenda.php
<?php
include('util/config.php');
$data = $_POST['data_submit'];
$local = utf8_decode($_POST['local']);
$cidade = utf8_decode($_POST['cidade']);
$agenda = new Agenda($data, $local, $cidade);
$agendaDAO = new AgendaDAO();
$agendaDAO->add($agenda);
foreach($agendaDAO->select("SELECT * FROM agenda ORDER BY id DESC LIMIT 1") as $rowA){
echo '<tr><td>'.$rowA['data'].'</td><td>'.utf8_encode($rowA['local']).'</td><td>'.$rowA['cidade'].'</td><td><i class=icon-edit></i> <i class=icon-remove></td></tr>';
}
?>
Try changing the dataType: 'text' to dataType: 'html'.

Categories