I am trying to remove table row after successful ajax call, but it is not working. No errors in firebug, it deletes the row on the back-end PHP, just doesnt remove the tr. Here is my code:
function closelead(rowid){
var rowid = rowid;
$.ajax({
type: "POST",
url: "ajax/close.php",
data: "rowid="+ rowid,
success: function(html){
$(this).closest('tr').remove();
}
});
}
and the html:
<table id="companytable">
<tr id="top"><th>Business Name</th><th>Phone</th><th>Carrier</th><th>X-Date</th><th></th><th></th><th></th></tr>
<?php
$query = "SELECT * FROM leads WHERE user = '$user' ORDER BY wccompcode";
$selectlead = mysql_query($query)or die(mysql_error());
while($leadlist = mysql_fetch_array($selectlead)){
$compcode = $leadlist['wccompcode'];
$compcode = sprintf("%03s", $compcode);
$selcomp = mysql_query("SELECT carname FROM carrierlist WHERE carcode = '$compcode'")or die(mysql_error());
while($carrier = mysql_fetch_array($selcomp)){
$carrier1 = $carrier['carname'];
}
?>
<tr id="<?php echo $leadlist['ID'];?>"><td id="busname"><?php echo $leadlist['busname'];?></td><td><?php echo $leadlist['phone'];?></td><td><?php echo $carrier1;?></td><td><?php echo date("m/d/Y",strtotime($leadlist['wcxdate']));?></td><td><input type="button" value="Call Back" class="searchbutton" /></td><td><input type="button" onclick="closelead(<?php echo $leadlist['ID'];?>)" value="Close" class="searchbutton" /></td><td><input type="button" value="Soft Quote" class="searchbutton" /></td></tr>
<?
}
?>
</table>
Try
function closelead(rowid){
var rowid = rowid;
$.ajax({
type: "POST",
url: "ajax/close.php",
data: "rowid="+ rowid,
success: function(html){
$('#'+rowid).remove();
}
});
}
function closelead(rowid){
var rowid = rowid;
$.ajax({
type: "POST",
url: "ajax/close.php",
data: "rowid="+ rowid,
success: function(html){
$('#'+rowid).closest('tr').remove();
}
});
}
Related
I've got a table of which I'm trying to add inline-edit to, but I'm really new to ajax. The JQuery code itself works, I can see the td's changing into input fields but I'm having trouble with posting the data.
When I click away from the table td that I'm trying to edit, it's suppose to post the data. But when I do that and refresh the page, the generated table isn't updated nor can I see changes in the database. It doesn't display an error, it only refreshes the page.
projectlist database table
projectid | Klant
4 | mike
12 | peterson
PHP
while($row = mysql_fetch_array($resultaat, MYSQL_ASSOC)){
$id = $row['projectid'];
<tr id="<?php echo $id ?>" class="tredit">
<form action="" method="post">
<td>
<span id="klant_<?php echo $id ?>" class="text"><?php echo $row["Klant"] ?></span>
<input type="text" class="ip" id="klant_ip_<?php echo $id ?>" value="<?php echo $row["Klant"] ?>">
</td>
</form>
</tr>
}
Ajax
$(document).ready(function(){
$(".tredit").click(function(){
var ID=$(this).attr('id');
$("#klant_"+ID).hide();
$("#klant_ip_"+ID).show();
}).change(function(){
var ID=$(this).attr('id');
var first=$("#klant_ip_"+ID).val();
var dataString = 'id='+ ID +'&Klant='+first;
//alert(dataString);
if(first.length > 0){
$.ajax({
type: "POST",
url: "post_table.php",
//data: dataString,
data: dataString,
cache: false,
success: function(html){
$("#klant_"+ID).html(first);
},
error: function (request, error) {
console.log(arguments);
alert(" Can't do because: " + error);
}
});
}else{
alert('Input something');
}
});
$(".ip").mouseup(function() {
return false
});
$(document).mouseup(function(){
$(".ip").hide();
$(".text").show();
});
});
post_table.php
<?php
include('config.php');
$klant = $_POST['Klant'];
$id = $_POST['id'];
$query = "update projectlist set Klant='$klant' where id='$id'";
mysql_query($query, $con);
?>
Solution
I got it working. It never ever occured to me to thoroughly check the post_table.php.
where id='$id' is wrong, it must be where projectid='$id'.
$(document).ready(function(){
$(".tredit").click(function(){
var ID=$(this).attr('id');
$("#klant_"+ID).hide();
$("#klant_ip_"+ID).show();
})
$(".tredit").change(function(){
var ID=$(this).attr('id');
var first=$("#klant_ip_"+ID).val();
var dataString = {"id":ID,"Klant":first};
//alert(dataString);
$("#klant_"+ID).html('<img src="load.gif" />');
if(first.length > 0){
$.ajax({
type: "POST",
url: "post_table.php",
data: dataString,
cache: false,
success: function(html){
$("#klant_"+ID).html(first);
}
});
}else{
alert('Input something');
}
});
$(".ip").mouseup(function() {
return false
});
$(document).mouseup(function(){
$(".ip").hide();
$(".text").show();
});
});
The php has to return something on the page
<?php
include('config.php');
$klant = $_POST['Klant'];
$id = $_POST['id'];
$query = "update projectlist set Klant='$klant' where id='$id'";
mysql_query($query, $con);
echo "success";
?>
This is my ajax call code..
<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();
$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
alert(data)
$("#desc").val(data);
$("#bal").val(data);
}
});
});
});
</script>
include "db.php";
$itemcode=$_POST['itemcode'];
$sql="select * from itemmaster where Item_Code='$itemcode'";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_array($result)) {
echo $row['Item_Desc'];
echo $row['Balance_Stock'];
}
This is simple html form..
<form action="add.php" method="post">
<table style="border: 1px solid black;padding:20px;"cellspacing="1px">
</br></br>
<tr>
<td>Issue No:</td> <td><input name="issueno" type="text" id="issueno"/></td>
<td>Issue Date:</td> <td><input name="issuedate" type="date" id="issuedate"/></td></tr></br></br><tr></tr>
<tr>
<td>Item Code:</td> <td><input name="itemcode" type="text" id="itemcode" /></td>
<td>Description:</td> <td><input name="des" type="text" style="width:250px; height:23px" id="desc"/></td>
<td>Bal Quantity:</td> <td><input name="bal" type="text" id="bal"/></td>
</tr></br>
<tr> <td>Issue Quantity:</td> <td><input name="issuequ" type="text" id="issuequ"/></td></tr></br><tr></tr>
<tr><td>Remark:</td> <td><input name="remark" type="text" style="width:250px; height:23px" id="remark"/></td></tr></br>
<tr><td colspan="6"><center><input type="submit" value="submit"></center></td></tr>
</table>
</form>
When I alert(data) I am getting this samsung20.00. where samsung is description and 20.00 is bal. I want to assign description desc id an bal to bal id. So How do I do that??
In your ajax.php file, you need to use json_encode function so you can parse it after get response:
include "db.php";
$itemcode=$_POST['itemcode'];
$sql="select * from itemmaster where Item_Code='$itemcode'";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_array($result)) {
$json = array("Item_Desc" = > $row['Item_Desc'],
"Balance_Stock" => $row['Balance_Stock']
);
}
echo json_encode($json);
After making adjusts that you can encode your response, Your ajax need to parse it.
Example:
<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();
$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
var obj = JSON.parse(data);
$("#desc").html(obj.Item_Desc);
$("#bal").html(obj.Balance_Stock);
}
});
});
});
Its very simple.
Hope it helps you
in your php file join the value Samsung and 20.00 with || sign
include "db.php";
$itemcode=$_POST['itemcode'];
$sql="select * from itemmaster where Item_Code='$itemcode'";
$result = mysql_query($sql, $con);
while($row = mysql_fetch_array($result)) {
echo $row['Item_Desc'].'||'.$row['Balance_Stock'];
}
java script code add the var response = data.split('||'); function
<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();
$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
alert(data)
var response = data.split('||');//spilt the value
$("#desc").val(response[0]);
$("#bal").val(response[1]);
}
});
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$('#itemcode').blur(function(){
var itemcode = $(this).val();
$.ajax({
type: "POST",
url:'ajax.php',
data: {'itemcode' : itemcode} ,
cache: false,
success: function(data) {
alert(data)
var str1 = data.replace(/\d.+/g, '');
var str2 = data.replace(/[^\d.-]/g, '');
$("#desc").val(str1);
$("#bal").val(str2);
}
});
});
});
</script>
i want to populate two fields when some one fill up card no.
<input type="text" id="name" name="name" class="form-control" Value="<?php echo $grow['name']; ?>">
<input type="text" id="address" name="address" class="form-control" Value="<?php echo $grow['address']; ?>">
but this code populate field one by one. can any one suggest be better code for populating two fields from database. Thank you
jquery
<script type="text/javascript">
$(document).ready(function()
{
$("#krishi").keyup(function()
{
var k=$(this).val();
var q="name";
$.ajax
({
type: "POST",
url: "getresult.php",
data: 'k='+k+'&q='+q,
cache: false,
success: function(data)
{
if(data){
$("#name").val(data);
$.ajax
({
type: "POST",
url: "getresult.php",
data: 'k='+k+'&q=address',
cache: false,
success: function(data)
{
if(data){
$("#address").val(data);
}
}
});
}else
$("#name").val("");
$("#address").val("");
}
});
});
});
</script>
getresult.php
<?php
define('INCLUDE_CHECK',true);
include("mysql.php");
$k=$_POST['k'];
$q=$_POST['q'];
$sql=mysql_query("select * from inward where krishi='$k'");
$row=mysql_fetch_array($sql);
echo $row[$q];
?>
Try to extract both name and address from database and json them
$k=$_POST['k'];
$q=$_POST['q'];
$sql=mysql_query("select address,name from inward where krishi='$k'");
$row=mysql_fetch_array($sql);
$result = array(
'name'=>$row['name'],
'address'=>$row['address']);
echo json_encode($result);
After that parse them via jquery
$.ajax
({
type: "POST",
url: "getresult.php",
data: 'k='+k+'&q=address',
cache: false,
success: function(data)
{
if(data){
var parsedData = jQuery.parseJSON(data);
$("#name").val(parsedData.name);
$("#address").val(parsedData.address);
}
}
});
Javascript Code:
<script type="text/javascript">
$(document).ready(function()
{
$("#krishi").keyup(function()
{
var k = $(this).val();
var q = "name";
$.ajax({
type: 'POST',
url: "getresult.php",
data: 'k='+k,
cache: false,
success: function(data)
{
var jsonArr = $.parseJSON(data);
if(typeof response =='object')
{
$("#name").val(jsonArr.name);
$("#address").val(jsonArr.address);
}
else
{
$("#name").val("");
$("#address").val("");
}
}
});
});
});
</script>
PHP Code:
<?php
define('INCLUDE_CHECK',true);
include("mysql.php");
$k = $_POST['k'];
$sql = mysql_query("select * from inward where krishi='$k'");
$row = mysql_fetch_assoc($sql);
echo json_encode(array('name' => $row['name'], 'address' => $row['address']);
?>
I have two different buttons in my page. clicking on the first one must call a php file named derive_rules.php while cliking on the second one must call another file named derive_rules1.php using ajax. For same i tried following code. But it is not working,
<script>
$(document).ready(function() {
$(".deriver").click(function() {
var val = $("#keyword").val();
var agent = $(this).attr('rel');
if(val == '') {$("#keyword").focus(); return;}
else {
$.ajax ({
url: 'derive_rules.php',
data: 'kw='+val+'&agent='+agent,
type: 'GET',
dataType: 'json',
cache: 'false',
success: function(data) {
if(data.success == 'true') {
$("#results_holder").show();
$("#results").html(data.msg);
$("#keyword").val('');
}
else {
alert(data.msg);
}
}
});
}
});
});
</script>
and those are mu buttons
<td><b>+ New Rule</b></td>
<td><input type = "text" name = "Keyword" id = "keyword" style = "width:300px" placeholder = "Keyword or Phrase"/></td>
<td><input type = "button" value = "Verify" class = "Buttons deriver" rel = "<?php echo $_GET['id']; ?>"/></td>
<td><input type = "button" value = "Add" class = "Buttons deriver" rel = "<?php echo $_GET['id']; ?>"/></td>
</tr></table>
what changes should i apply on my code to make it work???? as i want
Here is the quick solution.
HTML
<button class="same" data="derive_rules">Button One</button>
<br/>
<button class="same" data="derive_rules1">Button Two</button>
jQuery
$(".same").click(function(){
var url = $(this).attr("data");
//alert(url);
$.ajax ({
url: url+".php",//pass the url here or you can use whatever I used .php. And do the other stuff etc.
});
});
For more go to my JSFILLDE
If you need my help. Please find me any of social network by searching yeshansachithak.
Here Is Your Code - As You Want - For more explain
<table>
<tr>
<td><b>+ New Rule</b></td>
<td><input type="text" name="Keyword" id="keyword" style="width:300px" placeholder = "Keyword or Phrase"/></td>
<td><input type="button" data="your_file_name" value="Verify" class="Buttons deriver" rel="<?php echo $_GET['id']; ?>"/></td>
<td><input type="button" data="your_file_name" value="Add" class="Buttons deriver" rel="<?php echo $_GET['id']; ?>"/></td>
</tr>
</table>
Just pass another attribute in your <button data="your_file_name" ...... Then use your ajax call as like you did. Your button class is driver. Please see below.
<script>
$(document).ready(function() {
$(".deriver").click(function() {
//Edit by Yesh
var url = $(this).attr("data");
//To check
alert(url);
var val = $("#keyword").val();
var agent = $(this).attr('rel');
if(val == '') {$("#keyword").focus(); return;}
else {
$.ajax ({
//url: 'derive_rules.php', //Edit by Yesh
url: url+'.php',//You can use whatever extension (.html ect..)
data: 'kw='+val+'&agent='+agent,
type: 'GET',
dataType: 'json',
cache: 'false',
success: function(data) {
if(data.success == 'true') {
$("#results_holder").show();
$("#results").html(data.msg);
$("#keyword").val('');
}
else {
alert(data.msg);
}
}
});
}
});
});
</script>
Thanks dude.
If you need my help. Please find me any of social network by searching yeshansachithak.
look at classes on button , need to change selector
$(document).ready(function() {
$(".deriver0").click(function() {
var val = $("#keyword").val();
var agent = $(this).attr('rel');
if(val == '') {$("#keyword").focus(); return;}
else {
$.ajax ({
url: 'derive_rules.php',
data: {kw:val,agent:agent},
type: 'GET',
dataType: 'json',
cache: 'false',
success: function(data) {
alert(data);
}
});
}
});
$(".deriver1").click(function() {
var val = $("#keyword").val();
var agent = $(this).attr('rel');
if(val == '') {$("#keyword").focus(); return;}
else {
$.ajax ({
url: 'derive_rules1.php',
data: {kw:val,agent:agent},
type: 'GET',
dataType: 'json',
cache: 'false',
success: function(data) {
alert(data);
}
});
}
});
});
</script>
<td><b>+ New Rule</b></td>
<td><input type = "text" name = "Keyword" id = "keyword" style = "width:300px" placeholder = "Keyword or Phrase"/></td>
<td><input type = "button" value = "Verify" class = "Buttons deriver deriver0" rel = "<?php echo $_GET['id']; ?>"/></td>
<td><input type = "button" value = "Add" class = "Buttons deriver deriver1" rel = "<?php echo $_GET['id']; ?>"/></td>
</tr></table>
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