refreshing a div using ajax, my div didn't refresh - javascript

after deleting some data using ajax, i want to refresh my div. I am using Laravel 5.4 . I am a beginner in laravel. Please help me about this.
This is my code in refreshing a div :
var url = window.location.href;
function RefreshDiv() {
$(".uploaded-images").fadeOut();
$(".uploaded-images").load(url+" .uploaded-images", function() {
$(".uploaded-images").fadeIn();
});
}
When i execute the RefreshDiv function , it only makes my div empty, but when i press f5 to refresh my page, it successfully deleted my data. My only problem is my RefreshDiv where i wan't it to refresh my div but it gives me empty div.
Here is my delete function that executes well:
function deleteImage(product_img_id) {
$.ajax({
url: "{{ url('admin/del-img') }}/" +product_img_id,
type: "GET",
dataType: "JSON",
success: function(data) {
RefreshDiv();
}
});
}
here is my div that i want to refresh:
<div class="row" style="margin-top: 20px;">
<h5>Uploaded Images</h5>
<div class="uploaded-images">
</div>
</div>
inside of my uploaded-images div , is a delegate function from my other ajax.
Here is my delegate function :
function getProductDetails(product_id) {
$( ".uploaded-images" ).empty();
$.ajax({
url: "{{ url('admin/get-product') }}/" +product_id,
type: "GET",
dataType: "JSON",
success: function(data) {
for(var i=1;i<data.product_images.length;i++) {
$(".uploaded-images").append('<div class="img-wrap">'+'<span class="close">×</span>'+'<img src="'+"{{ asset('image_files') }}/"+data.product_images[i].product_image+'" data-id="'+data.product_images[i].product_img_id+'" style="max-height:50px; max-width:90px;">'+'</div>');
};
}
});
}

Why you want to refresh the div...If you're removing the image why not just remove the element on success from DOM?
Try this:
function RefreshDiv(product_img_id) {
$("img[data-id="+product_img_id+"]").closest('.img-wrap').remove();
}

Try this:
function deleteImage(product_img_id) {
$.ajax({
url: "{{ url('admin/del-img') }}/" +product_img_id,
type: "GET",
dataType: "JSON",
success: function(data) {
RefreshDiv(product_img_id);
}
});
}
var url = window.location.href;
function RefreshDiv(product_img_id) {
$(".uploaded-images img[data-id="+product_img_id+"]").parent().remove();
$(".uploaded-images").load(url+" .uploaded-images", function() {
$(".uploaded-images").fadeIn();
});
}

Related

get id from template php to model (MVC)

I have a Project using MVC, in file chocanh.php , I want to get the id when clicking the button and return the id to the file muahang.php in the model.
Display in website code in chocanh.php code in muahang.php (model/muhang.php)
chocanh.php
<a id="11" onclick="getId(this)">OK</a>
<script>
function getId(input) {
$.ajax({
type: "POST",
url: "/model/muhang.php",
data: { id: input.id },
dataType: "json",
success: function(response) {
if(data === true) {
console.log("success")
}
}
});
}
</script>

Replace entire div instead of div contents

i am using the following ajax script to replace the contents of a div. But it only changes the contents inside that div. I want to replace the entire div.
<script>
function sendmessage()
{
var message_content=$("#message").val();
$.ajax({
type: "POST",
url: "newmessage.php",
dataType: "html",
data: {message:message_content},
success: function(data) {
$("#newmessage").html(data);
}
});
}
</script>
Here contents of id="newmessage" is changed. I want to replace ... with ...
Use replaceWith
<script>
function sendmessage()
{
var message_content=$("#message").val();
$.ajax({
type: "POST",
url: "newmessage.php",
dataType: "html",
data: {message:message_content},
success: function(data) {
$("#newmessage").replaceWith(data);
}
});
}
</script>
Use .replaceWith() :
$("#newmessage").replaceWith(data);

refresh page with ajax jquery without use of reload function

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.

Enable a div based on the response from an ajax

I have an ajax call and i need to show one particular div based on the response from the ajax call. here is my ajax call
var cmnumber = document.forms['myform']['cm'].value;
alert(cmnumber)
$.ajax({
type:'get',
url: "/validatecm/"+cmnumber,
cache:false,
async:true,
data:cmnumber,
success: function(data) {
},
error: function(data) {
}
})
});
I need to show on div if the response is success. Default the div is Hidden.
<div class="downtime" id="downtime" style="display: none" >
--------------
</div>
Any help wil be appreciated..
try this
var cmnumber = document.forms['myform']['cm'].value;
alert(cmnumber)
$.ajax({
type:'get',
url: "/validatecm/"+cmnumber,
cache:false,
async:true,
data:cmnumber,
success: function(data) {
if(data == "success")
$('#downtime').show();
else
$('#downtime').hide();
},
error: function(data) {
}
})
I think you can use
$.ajax({
type:'get',
url: "/validatecm/"+cmnumber,
cache:false,
async:true,
data:cmnumber
}).done(function(data){
$('#downtime').show();
})
Add the data into the div and show it.
success: function(data) {
$('#downtime').html(data).show();
},
error: function(data) {
$('#downtime').html().hide();
}
document.getElementById("downtime").style.display = "block";
See https://developer.mozilla.org/en-US/docs/CSS/display
get the value of text box and save it in cmnumber. Make sure you include jquery in your html file
function makeAjaxCall(){
var cmnumber=$("#cm").val();
var url="/validatecm/"+cmnumber;
$.ajax({url:url,success:function(result){
handleResult(result);
}});
}
function handleResult(result){
// if result is what you expect it is then enable the div
if(result=="ok"){
$("#downtime").css('display','block');
}
}
I'm new to AJAX but you have to change one thing to hide/show a the div:
var cmnumber = document.forms['myform']['cm'].value;
alert(cmnumber)
$.ajax({
type:'get',
url: "/validatecm/"+cmnumber,
cache:false,
async:true,
data:cmnumber,
success: function(data) {
if(data.**MESSAGE** == "success")
$('#downtime').show();
else
$('#downtime').hide();
}, error: function(data) { } })

jquery ajax sucess data into facebox

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

Categories

Resources