I am performing delete records by using jquery ajax in php. I want to refresh that content without the use of location.reload() function. I tried this,
$("#divSettings").html(this);
but, it's not working. What's the correct logic to get updated content in div.
Thanks.
Code:
function deletePoll(postId){
$.ajax({
type: "POST",
url: "../internal_request/ir_display_polls.php",
data: {
postId: postId
},
success: function(result) {
location.reload();
//$("#divSettings").html(this);
}
});
}
You're almost there:
function deletePoll(postId){
$.ajax({
type: "POST",
url: "../internal_request/ir_display_polls.php",
data: {
postId: postId
},
success: function(result) {
$("#divSettings").html(result); // <-- result must be your html returned from ajax response
}
});
}
You just needed to set the result into your '#divSettings' element using the .html() function :
$('#divSettings').html(result);
So a full example would look like :
function deletePoll(postId){
$.ajax({
type: "POST",
url: "../internal_request/ir_display_polls.php",
data: {
postId: postId
},
success: function(result) {
//Sets your content into your div
$('#divSettings').html(result);
}
});
}
I believe you have to clear the section first and then you can attach the HTML again. Like
function deletePoll(postId){
$.ajax({
type: "POST",
url: "../internal_request/ir_display_polls.php",
data: {
postId: postId
},
success: function(result) {
//Sets your content into your div
$('#divSettings').html("");
$('#divSettings').html(result);
}
});
}
I believe this way you won't see the old content.
Related
im using the function below to get image names. I also use the json code to get data of a different url, but somehow it isnt working at this. (im new to javascript. Just writing php normally.
function getImgname(name) {
$.getJSON("http://url.com/info.php?name="+name, function(json321) {
return json321.js_skininfo;
});
}
Try this:
function getImgname(myName) {
$.ajax({
url: 'http://url.com/info.php',
data: {
name: myName
},
type: 'POST',
dataType: 'json',
success: function(data) {
// do what you want with your data
return data.js_skininfo;
}
});
}
I tried this now:
function getImgname(myName) {
$.ajax({
url: "http://url.com/ninfo.php",
type: 'POST',
dataType: 'json',
success: function (data) {
return data.js_skininfo;
},
error: function () {
}
});
}
This isnt working (undefinied), but if i alert the data.js_skininfo it shows me the correct value.
I have this code in my asp.net webform, I call a webmethod to load data from database:
function Inicializar() {
$("#wait").show();
$.ajax({ type: "POST",
url: "index.aspx/Iniciar",
data: JSON.stringify({}),
contentType: 'application/json; charset=utf-8',
dataType: "json",
success: function (response) {
$("#wait").hide();
var result = response.d;
cargardatos(result );
}
});
}
I call this function in ready event:
$(document).ready(function () {
Inicializar();
});
the first time it works, the problem is that when I press F5 and reload the page data is not loaded, and if I press back working again.
I do not know if I'm doing something wrong.
Sorry for my English..Xd
Thank you. Greetings!
JavaScript is CaSe SenSitIve. So change your code to:
$(document).ready(function () {
Inicializar();
});
But this way, it doesn't even work on the first time. And do check what is wrong with your output using the Network or Console.
Disable Caching if Needed
$.ajax({ type: "POST",
url : "index.aspx/Iniciar",
cache : false,
datatype : 'json'
})
Turn off caching
function Inicializar() {
$("#wait").show();
$.ajax({ type: "POST",
url : "index.aspx/Iniciar",
cache : false,
datatype : 'json'
}).done(function (response) {
$("#wait").hide();
cargardatos(response.d);
});
}
$(document).ready(function () {
Inicializar();
});
I have the following code:
$.ajax({
type: 'GET',
url: 'edit_slides.php',
data: { user_id: user_id },
success: function() {
location.reload();
$('#messageContent').append('<p>success</p>');
}
});
What happens right now is that it appends and then reloads real quick so it loses the message.
Now, I could take out the reload and it will append but in order for my changes to take affect, the page needs to be reloaded.
Is there anything else I can do? I want to show a success message after reloading the page.
Thanks!
location.reload() will reload your page, which creates a new request to your server and the browser then loads the response. You could add a parameter to the query string for the message, e.g.
$.ajax({
type: 'GET',
url: 'edit_slides.php',
data: { user_id: user_id },
success: function() {
window.location = "?message=success";
}
});
Then in server side code, you could display the message if the parameter exists. In ASP.NET MVC, it would look like this:
#if(Request["message"] == "success")
{
<p>success</p>
}
If you don't have server side scripting available, you can also parse the querystring with javascript.
You can have some delay before you reload the page. Try this
$.ajax({
type: 'GET',
url: 'edit_slides.php',
data: { user_id: user_id },
success: function() {
$('#messageContent').append('<p>success</p>');
setTimeout(function(){
location.reload();
}, 500);
}
});
Send it to a location but with a hash in the url to indicate a success:
$.ajax({
type: 'GET',
url: 'edit_slides.php',
data: { user_id: user_id },
success: function() {
window.location = window.location + "#success";
}
});
Now when the page loads, check for the hash:
$(document).ready(function(){
if(window.location.hash) {
$("#messageContent").append("<p>success</p>");
}
UPDATE: The code if working I has some css issues.
I'm trying to put ajax data into facebox modal box, I have the following code but facebox modal box is not loading. Looking into firebug ajax is returning the correct data but i do not know how to pass that data to facebox.
$('a[rel*=facebox]').live("click", function() {
var ajaxpostID=$(this).parent().attr("id"); //Get entry ID
$.ajax({
url: 'http://www.someurl.com/ajax/facebox-ajax.php',
type: "POST",
data: ({
ajaxpostID: ajaxpostID
}),
success: function(data) {
$.facebox(data);
},
error: function() {
$.facebox('There was an error.');
}
});
});
Something like this worked for me:
//added some id to anchor tag and
$('a[id='some_anchor_id']').live("click", function() {
var ajaxpostID=$(this).parent().attr("id"); //Get entry ID
jQuery.facebox(function() {
var form_data = {
ajaxpostID: ajaxpostID
};
$.ajax({
url: "http://www.someurl.com/ajax/facebox-ajax.php",
type: 'POST',
data: form_data,
success: function(data) {
jQuery.facebox(data);
},
error: function() {
$.facebox('There was an error.');
}
)
});
})
})
Hope it works for you
$.ajax({
url: 'http://localhost/apsd4/core/main_quota.html',
success: function(data) {
$('#tabs-2').html(r);
$('#tabs-2 #nini').remove();
}
});
I would like to change data response before echo data
Stages:
remove '#tabs-2 #nini'
html()
I think as what I understand on your problem, you wanted to improve your $.ajax
$.ajax({
type: "POST",
url: "http://localhost/apsd4/core/main_quota.html",
success: function(result) {
$('#tabs-2').html(data).find('#nini').remove();
// do some code here
}
});