how to perform multiple ajax queries within an ajax block - php

am working on a project in which i use ajax, jquery, php and sql to manipulate data.
function fetchComments(commentOnId, commentOn){
if(!isNaN(commentOnId) && commentOnId >=0){
$.ajax({
type: 'POST',
dataType: "json",
url: 'get_comments.php',
data: 'commentOnId='+commentOnId+'&commentOn=blogArticle',
success: function(data){},
error: function(){
}
});
}
}
the above function uses the 'commentOnId & commentOn' passed to it to fetch all comment on that that particular blog id in json format.
the result/rows in my comment table are as follows.
id
commentOnId
commentOn
commentText
authorId
created
what i want to do is get the values of these rows and in this same function run another ajax query which i will pass the authorId to a php file that returns an authors's full name from a table called authorDetails,
and also use the id value to run another ajax query to return all replies on this comment id or null if there is no reply. and finally update a html div '' with the result.
please, i will appreciate your quick comprehensive assistance. thank you.

You can use SQL joins to get all your needed data in one request if you don't want other details on user iterative basis.

If I were you, I would fetch everything in a single Ajax call rather than perform another Ajax call.
If you want multiple Ajax calls, you can do the following.
function fetchComments(commentOnId, commentOn){
if(!isNaN(commentOnId) && commentOnId >=0){
$.ajax({
type: 'POST',
dataType: "json",
url: 'get_comments.php',
data: 'commentOnId='+commentOnId+'&commentOn=blogArticle',
success: function(data){
$.ajax({
type :'post',
url : 'get_auth_details.php',
data : 'authorId=' + data.authorId,
success : function(data) {
Update div logic goes here
}
}
},
error: function(){
}
});
}
}

Related

Need help in Ajax

I have this code to POST value to main.php, with onclick function UserID will be called and value which is user_id should be changed based on selected value and load selected user_id in user_profile.php?ID=user_id
var USER_ID = 0;
function UserID(user_id)
{
USER_ID = user_id;
$.ajax({
type: "POST",
url: "main.php",
data: {ID: USER_ID},
success: function(msg)
{
alert(USER_ID);
$('#user_profile_content').load('user_profile.php?ID=USER_ID #user_profile_content');
},
error: function()
{
alert("error");
}
});
}
After success post, alert is showing with selected value, it means POST was successful but I don't know how to make user_profile.php?ID=USER_ID to be changed based on received value. It's not replace global variable USER_ID.
From what I understand you want to change the url route, try:
...
success: function(msg){
window.location.href = `user_profile.php?${msg.id}`
$('#user_profile_content').load(`user_profile.php?ID=${msg.id}
#user_profile_content`);
...
ES6 template literals allow you to insert the returned value in a string.
Window.location.href allows you to change the route on the browser (Frontend), I recommend you do it from the Backend if possible though.
This answer assumes msg.id is the desired value that you want to pass since the contents of the response are not specified.

Get the class name from returned value of POST in Ajax

I have two dropdown lists and on selecting the data I used ajax to send it to a php file where I retrieved a table and send the whole table contents as per my query fields and I display it via
jQuery("div#tablecontent").html(returnval);
But now i want to edit, delete the table view I displayed and I tried to get the class of the row I returned. But couldn't please guide me in how to get the class of the field I returned as whole table.
EDIT : Adding the code i ve done
jQuery(document).ready(function(){
jQuery("#select1").change(function(){
jQuery.ajax({
type: "POST",
url: "<?php echo $base_url;?>?q=search/won",
error: function(returnval) {
alert("Failure");
},
success: function (returnval) {
// alert(returnval);
jQuery("select#fileds_content").html(returnval)
//alert("Sucess");
}
})
//
jQuery("#fileds_content").change(function(){
if(jQuery(this).val()){
var datawon = jQuery(this).val();
jQuery.ajax({
type: "POST",
url: "<?php echo $base_url;?>?q=getbases/won",
data:{ datawon : datawon},
error: function(returnval) {
// alert(returnval);
// alert("Failure");
},
success: function (returnval) {
// alert(returnval);
jQuery("div#tablecontent").html(returnval);
//alert("Sucess");
}
})
I am not entirely sure what you actually want to do, but from what I understood is that you cannot select a newly created element by its class. In that case, you cannot select a newly created elements because js does not know about it yet, thus, you can use something like .ajaxComplete(), this will make sure to run a function After an ajax call got completed.

using Ajax can i call one function in many times in php?

I want download big data from database
so using ajax call single function who pass limit to download database,
ex-66000 records
can I pass to ajax function values 20000,20000,26000
this way and download database.
thank you
Well I think, you can use nested AJAX type structure.
Call the second function when the first function accomplish its AJAX call.
Something Like this:
$.ajax({
type: 'GET',
url: "/dosomething",
cache: false,
dataType: "html",
data: { lowerbound:"0"; uperbound: "2000"} ,
success: function(html_input)
{
$.ajax({
type: 'GET',
url: "/dosomething",
cache: false,
dataType: "html",
data: { lowerbound:"2001"; uperbound: "4000"} ,
success: function(html_input){
alert(html_input);
}
});
}
});
I used for 2 iterations.
Another way is that you can pass the variables recursively to the function and perform the desired task. This will help you to scale the functions with n numbers of time.

CStarRating On Click Save

I am using CStarRating in my Yii project. What at present I have to do is set rating and then hit submit.This works very well but i want data in db to get update in single Go. I have tried various Ajax based tricks but I am new so I fail.
here is my existing code
$this->widget('CStarRating',array(
'model'=>$model,
'attribute'=>'hotel_rating',
'readOnly'=>FALSE,
));
I have tried following ajax one
$this->widget('CStarRating',array(
'name'=>'rating',
'model'=>$model,
'attribute'=>'hotel_rating',
'callback'=>'
function(){
$.ajax({
type: "GET",
url: "'.Yii::app()->createUrl('hotel/update').'",
data: "id='.$model->id.'&rate=" + $(this).val(),
success: function(msg){
alert("Sucess")
},
error: function(xhr){
alert("failure"+xhr.readyState+this.url)
}
})}'
));
in controller I do but still unlucky
$model->hotel_rating = $_GET['rate']
$model->save()
In controller, I guess it should be $model->hotel_rating = $_GET['rate'], and similarly $_GET['id'] will return the id of the model.

jQuery, need help with 'sortreceive' ajax call and POST vars

I have a dilemma that just seems beyond my abilities at the moment!
I have a group of connected sortables using the class 'biglist'.
What I want to do is bind #biglist 's sortreceive callback (which is made whenever a list receives an element from another) to take the 'boxnum' value of the element (which signifies which list its coming from) and perform an UPDATE query changing the id's boxnum value from say 5(list it came from) to 7 (list its been dragged to) so that the state persists.
So the exchange would happen like so (roughly)
$( "#biglist" ).bind( "sortreceive", function(event, ui) {
ajax call to boxchange.php
create vars to represent elements 'boxnum' value and 'box moved to' value
});
Then inside boxchange.php ->
$id = $_POST['id']
$box = $_POST['boxnum']
->update query SET boxid to new boxid WHERE id = posted ID of element
I hope this makes sense. It seems like a pretty slick way to make my program work!
Any help is greatly appreciated.
EDIT:
Just cleaned up the function to see if there are any changes that need to be made to it (which I know there are, because it looks sloppy) This function would need to be copied/altered for each sortable separately but it'd totally make the program work at least!
function ReceiveTwo()
{
$('#sortable2').bind('sortreceive', function(event, ui)
{
boxnum = $(this).attr('boxnum');
id = $(this).attr('id');
$.ajax
({
url: "boxchange.php",
type: "POST",
data: boxnum, id,
success : function(feedback)
{
$('#data').html(feedback)
}
})
});
$('#sortable2').sortable("refresh");
});
$('#sortable2').bind('sortreceive', function(event, ui) {
$.ajax({
url: "boxchange.php",
type: "POST",
beforesend: function(){
boxnum = $(this).attr('boxnum');
id = $(this).attr('id');
},
data: {'boxnum': boxnum, 'id': id},
success : function(feedback) {
$('#data').html(feedback),
}
});
});
beforesend is the event that fires before the ajax call. I believe here you could set your properties to accomplish what you want.
I think the way you want to send your Javascript data to your server-side PHP script is using a Javascript associative array, like so:
$.ajax({
url: "boxchange.php",
type: "POST",
data: {'boxnum': boxnum, 'id': id},
success: function(data,status) { ... }
Your "boxchange.php" script would then be able to access those variables via $_POST['boxnum'] and $_POST['id'].
I think that was your goal, but I'm not entirely sure...

Categories