AJAX Post two id - php

can I send two id in ajax post? can i make like this? but this script not work..
<a href='#' class='plyshr' id1="<?php echo $tracks['track_id']; ?>" id2="<?php echo $row[2]; ?>">
<?php echo $row[2]; ?>
</a>
and in AJAX
some more details
$(document).ready(function(){
$(".plyshr").click(function () {
var id = $(this).attr("id");
var dataString = 'id1=' + id1;
var dataString = 'id2=' + id2;
var parent = $(this);
$.ajax({
type: "POST",
url: "playlist.php",
data: dataString,
cache: false,
success: function (html) {
parent.html(html);
}
});
return false;
});
});

Assuming rest of the code is working.
var id1 = $(this).attr("id1"); // Get the first id.
var id2 = $(this).attr("id2"); // Get the second id.
var dataString = 'id1='+ id1 ;
dataString += '&id2='+ id2 ; // Set both in string with & separtor

var id1 = $(this).attr('id1'),
id2 = $(this).attr('id2'),
dataString = 'id1='+ id1 ;
dataString += '&id2='+ id2 ;

Why are you using custom attributes, you can use data attribute, like
<a href='#' class='plyshr' data-id1="<?php echo $tracks['track_id']; ?>"
data-id2="<?php echo $row[2]; ?>">
<?php echo $row[2]; ?>
</a>
SCRIPT
$(".plyshr").click(function () {
var self=this;
var parent = $(this);
$.ajax({
type: "POST",
url: "playlist.php",
// use jquery data function here
data:{id1: $(self).data('id1'),id2:$(self).data('id2')},
cache: false,
success: function (html) {
parent.html(html);
}
});
return false;
});

Related

Passing Variable Value from PHP to Ajax and Changing Attribute Value in HTML

My PHP is returning this data to Ajax...
echo $data6['favorite_properties_id'];
I am updating it in one function and trying to send it to another using following html and jquery .
<img class="<?php if($favorite == 1){ echo 'alreadyfavorite';} else { echo 'addtofavorite';} ?>" pid="<?php echo $propertyid; ?>" fpid="<?php while($data5=$select5->fetch()){echo $data5['favorite_properties_id'];} ?>" src="../images/system/addtofavorite.png">
This is my jquery...
$('.alreadyfavorite1').click(function() {
event.preventDefault();
var del_id = $(this).attr('fpid');
var $ele = $(this).parent().parent().parent();
var reference = this;
$.ajax(
{
type: 'POST',
url: '../controllers/favoritesaddremove.php',
data:
{
del_id: del_id
},
success: function(data)
{
$ele.fadeOut(1000).delay(1000).remove(1000);
}
});
});
// On Search Property Results Page - Add to Favorite Button (Heart)
$('.addtofavorite').click(function() {
event.preventDefault();
var ins_id = $(this).attr('pid');
var del_id = $(this).attr('fpid');
var reference = this;
/* alert(del_id);
alert(ins_id); */
$.ajax(
{
type: 'POST',
url: '../controllers/favoritesaddremove.php',
data:
{
ins_id: ins_id
},
success: function(data)
{
$(reference).toggleClass("addtofavorite alreadyfavorite");
$('.alreadyfavorite').attr('fpid', data);
}
});
});
The second function is not working, but if i refresh the page then the second function is working...
First assign some id to the image and change fpid to data-fpid(data-attribute):
<img class="asdasd" id="aid" data-fpid="something">
In your success try:
success: function(data)
{
$('#aid').data('fpid', data); //this should update the value in data-fpid
}

Error when using jQuery Ajax POST (json) with php

On page load I use this code to display data from database:
<script>
$(function feed(){
var page = 1;
var type = '<?php echo $filter ;?>';
var theData = {};
theData['page'] = 'profile';
theData['type'] = type;
theData['username'] = '<?php echo $user_data->username; ?>';
theData['get_activities'] = 'true';
$.ajax({
type: "POST",
url: "data.php",
data: theData,
dataType:'json',
success: function(data){
$("#activities").html(data.activity_feed);
if(page < data.total_pages){
$("#activities").after('<div id="loader"><button id="load_more_activities">Load more</button></div>');
}
}
});
$("#activity_container").on("click", "#load_more_activities", function(){
var next = page+=1;
var type = '<?php echo $filter ;?>';
var theData = {};
theData['page'] = 'profile';
theData['type'] = type;
theData['username'] = '<?php echo $user_data->username; ?>';
theData['get_activities'] = 'true';
theData['page_num'] = next;
$.ajax({
type: "POST",
url: "data.php",
data: theData,
dataType: "json",
success: function(data){
$("#activities").append(data.activity_feed);
if(next == data.total_pages){
$("#loader").html("No more data");
} else {
$("#loader").html('<div id="loader"><button id="load_more_activities">Load more</button></div>');
}
},
});
});
});
</script>
Everything work fine, but if I refresh page more than 5 or 6 times or if load_more_activities function is called more than 5 or 6 times...then post is not executed and I don't get any data displayed...
Is something wrong with this code or there are maybe some restrictions from my host provider?
When post is executed:
When post is not executed:
My host provider has a protection from several sending ajax data with variables named "username" or "password". So when this happens, server detect some kind »brute-force« and then drop-all-packets.

Ajax call returns nothing

I am trying to make an ajax call on click on anchor tag dynmically generated from $.each loop for a JSON response.
For Information : #records is my table and .update is the class of anchor tag in that table.
Please be informed that the table is generated dynamically.
Now the problem is that my ajax call is returning nothing even i have checked it error: but no response received. I have tried alerting my var data just before the ajax call and it worked.So the problem starts from the ajax call. Moreover, my server side code is running fine.
// Update existing customers
$("#records").on('click', ".update", function() {
var data = '?'+ $(this).attr('id');
$.ajax({
type: "GET",
url: "viewcustomers.php",
data: data,
success: function(response) {
console.log(response);
}
});
});
Thanks in advance.
For reference below is the code that generates the table.
// Function to make datagrid
function getRecords() {
$.getJSON("viewcustomers.php", function(data) {
var items = [];
var xTd = '';
var xTr = '';
$.each(data, function(key, val) {
var c = 0;
var id = 0;
$.each(val, function(key1, val1) {
if (c == 0)
{
id = val1;
}
c++;
xTd += '<td>' + val1 + '</td>';
});
xTd += '<td>Edit</td>';
xTd += '<td>Delete</td>';
xTr = '<tr>' + xTd + '</tr>';
items.push(xTr);
xTd = '';
xTr = '';
});
$("#records").append(items);
});
}
Updated the server side code:
page url : localhost/hotel/viewcustomers.php
/**
* Fetch single row for the purpose of update / delete.
*/
if(isset($_GET['update'])){
$customer = new Customers;
$Id = $_GET['update'];
$customer_single = $customer->View_Single_Customer($Id);
echo json_encode($customer_single);
unset($customer);
}
This line is not used the right way var data = '?'+ $(this).attr('id');
Change it like this: var my_id = $(this).attr('id');
Then update the line data: data with data : {id:my_id}
Complete code :
$("#records").on('click', ".update", function() {
var my_id = $(this).attr('id');
$.ajax({
type: "GET",
url: "viewcustomers.php",
data : {id : my_id},
success: function(response) {
console.log(response);
}
});
});
Or do it like this:
$("#records").on('click', ".update", function() {
var param = '?id='+ $(this).attr('id'); /*notice that I have added "id=" */
$.ajax({
type: "GET",
url: "viewcustomers.php" + param,
/* remove the data attribute */
success: function(response) {
console.log(response);
}
});
});
Modify it as
$("#records").on('click', ".update", function() {
var request = '?id='+ $(this).attr('id');
$.ajax({
type: "GET",
url: "viewcustomers.php" + request,
success: function(response) {
console.log(response);
}
});
});

How do I get jQuery/AJAX result into table cell

I have this script here that outputs a result from a sql db query but I can not figure out how to get it to put the result in the table cell specified.
Here is query:
<script type="text/javascript">
$(document).ready(function(){
$('.typeval').change(function(){
var movement = $(this).val();
var client_id = $(this).parent().siblings().find('.clientval').val();
var class_id = <? echo $class_id; ?>;
$.ajax({
type: "POST",
url: "movement_count.php",
data: {movement:movement, client_id:client_id, class_id:class_id},
dataType: "json",
success:(function(output) {
$.each(output, function(index, value){
alert(value);
$(".count").append(output[value]);
}) // each
}) // success
}); // ajax
}); // .typeval
}); // document
</script>
I want to put the result of this (value):
alert(value);
into here:
<td><label class="count"></label></td>
The <td> I am targeting on the same row as the <select> menu that is triggering the result. There will also be multiple table rows with this exact same cell <td>. So I need to target the <td> on this row only somehow. Can someone help me with this? Im not sure if I need the $.each but my php query is a mysql_fetch_row array even though the value returned will always be just one number.
Here is the HTML markup for the table cell I need the value in:
<td><label class="count"></label></td>
JS source code for class=count:
$(".count").append(output[index]);
ITS WORKING!!!! here is the code below
$(document).ready(function(){
$('.typeval').change(function(){
var movement = $(this).val();
var client_id = $(this).parent().siblings().find('.clientval').val();
var class_id = <? echo $class_id; ?>;
$count = $(this).parents('tr').find('label.count');
$.ajax({
type: "POST",
url: "movement_count.php",
data: {movement:movement, client_id:client_id, class_id:class_id},
dataType: "json",
success:(function(output) {
$.each(output, function(index, value){
//alert(value);
$count.append(output[index]);
}) // each
}) // success
}); // ajax
}); // .typeval
}); // document
Replace this
$("label.count<?php echo $client_id; ?>").append(output[value]);
With
$("label.count<?php echo $client_id; ?>").append(value);
Or alternately with this
$("label.count<?php echo $client_id; ?>").append(output[index]);
Hope this will help !!
Working code:
$(document).ready(function(){
$('.typeval').change(function(){
var movement = $(this).val();
var client_id = $(this).parent().siblings().find('.clientval').val();
var class_id = <? echo $class_id; ?>;
$count = $(this).parents('tr').find('label.count');
$.ajax({
type: "POST",
url: "movement_count.php",
data: {movement:movement, client_id:client_id, class_id:class_id},
dataType: "json",
success:(function(output) {
$.each(output, function(index, value){
//alert(value);
$count.append(output[index]);
}) // each
}) // success
}); // ajax
}); // .typeval
}); // document

pass a href link value as variable to ajax

is it possible to pass the link value of a hyperlink to ajax?
I'm quite new in this branch so maybe I'm doing actually wrong
so I would like to achieve the following:
echo '<ul><li class="all">alle</li>';
foreach(range('a','z') as $i):{
echo '<li class = "searchAbc"><a class="button" href="#"/>'.$i.'</a></li> </ul>';
}
endforeach;
than the ajax
function ajax_abc() {
var url = 'index.php?option=com_glossary&task=abc';
var abc= $(this).attr('href');
// data = 'format=raw'+ '&'+'val=' + $("#search").val();
data = 'val=' + abc + '&' + 'format=raw';
$(document).ready(function(){
function showLoader(){
$('.search-background').fadeIn(200);
}
function hideLoader(){
$('#sub_cont').fadeIn(1500);
$('.search-background').fadeOut(200);
};
$(" li.edit a").click(function(){
ajax_redirect();
});
$(".searchBtn").click(function() {
ajax_search();
});
$('#search').keyup(function(e) {
if(e.keyCode === 13) {
ajax_search();
}
});
function ajax_search(search) {
var url = 'index.php?option=com_glossary&task=getvalues';
data = 'val=' + $("#search").val() + '&' + 'format=raw';
$('#sub_cont').fadeIn(1500);
showLoader();
$.ajax({
type: "GET",
url: url,
data: data,
success: function(data) {
hideLoader();
$('div.default_order').hide;
$('#sub_cont').html(data);
}
}); // ajax
}
function ajax_abc(abc) {
var url = 'index.php?option=com_glossary&task=abc';
data = 'val=' + abc + '&' + 'format=raw';
//... Add jQuery.load() codes here ...
$.ajax({
type: "GET",
url: url,
data: data,
success: function(data) {
hideLoader();
$('div.default_order').hide;
$('#sub_cont').html(data);
}
}); // ajax
}
$(".stripeMe tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
$(".stripeMe tr:even").addClass("alt");
});
do you mean something like this?
$('a.button', 'li.searchAbc').click(function(){
var url = 'index.php?option=com_glossary&task=abc';
var abc = $(this).attr('href');
var ajaxParam = {
val: abc,
format: 'raw'
};
$.post(url, ajaxParam, function(result){
//process result
});
// or using GET
/*
$.get(url, ajaxParam, function(result){
//process result
});
// */
return false;
})
note: attribute href of your <a> tag is '#'.
Ok try something like the following
Change
echo '<li class = "searchAbc"><a class="button" href="#"/>'.$i.'</a></li> </ul>';
to
echo '<li class = "searchAbc"><a class="button" href="#" onClick="ajax_abc(\''.$i.'\')"/>'.$i.'</a></li> </ul>';
Then put in your javascript
function ajax_abc(abc) {
var url = 'index.php?option=com_glossary&task=abc';
data = 'val=' + abc + '&' + 'format=raw';
//... Add jQuery.load() codes here ...
}
In my case this worked form me !!
function deleteImage(data1,data2){
$.ajax({
type: "POST",
url: "remove_unwanted_files.php",
data: "nameImage="+data1+"&idImage="+data2,
success: function(msg){
alert( "Data Saved: " + data1 + " " + data2);
//Code for Anything you want
}
});

Categories