Hello I am having trouble regarding div reloading when a new record has been added. What I wanted to do is to show first a loading image then after a record has been inserted it will reload the div.
$("a.follow").click(function(event) {
event.preventDefault();
$("#flash").show();
$("#flash").fadeIn(300).html('<img src="ajax-loader-transp.gif" />Loading Result.');
$.ajax({
//url: $(this).attr("href"),
success: function(msg) {
$("#results_div").load('http://localhost/<app_name>/index.php/<contoller>/index');
}
});
return false;
});
That's what I got to far when I'm trying the code it refreshes a whole physical of page on the div & not the desired div itself. . .
Sorry guys I am poor with jQuery and BTW this is in CodeIgniter.
Your problem is, that codeigniter obviously returns a whole html page. You have two choices:
Either return only a fragment (I don't know how to do this in CI) or use jQuery to parse out the div you want. This can be done with the following code, assuming that the div you want is named <div id="results_div">...</div>
$("a.follow").click(function(event) {
event.preventDefault();
$("#flash").show();
$("#flash").fadeIn(300).html('<img src="ajax-loader-transp.gif" />Loading Result.');
$("#results_div").load('http://localhost/<app_name>/index.php/<contoller>/index #results_div', function(){ $('#flash').hide(); });
});
Can you include the HTML with the #results_div div?
This is my best guess without html to work with:
$("a.follow").click(function(event) {
event.preventDefault();
// show the linke
$("#flash").fadeIn(300).html('Loading Result.');
//ajax load the 'mini div' result -- i guessed that this url pulls back the div you want
$("#results_div").load('http://localhost/<app_name>/index.php/<contoller>/index', function(data, text, xhr){
$('#flash').fadeOut("fast");
});
});
Related
I have a webpage where a user can post a question, the question is posted and on success response appends text to a div 'your question was posted.' Then the div fades away after 1 second.
If the user posts a second question (without refreshing the page), the question is successfully inserted to the database, but the text is not appending to the div.
Do I need to clear the timeout each time the users posts?
$(document).ready(function(){
$('#postquestion').on('submit',function(e) {
$.ajax({
url:'/question.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
if($.trim(data) == "success") {
$('<div align=\"center\" style=\"padding-top:10px\"><p style=\"font-weight:bold;font-size:14px\">Your question has been posted.</p></div>').appendTo('#product_question');
setTimeout(function() {
$('#product_question').fadeOut('fast');
}, 1000);
}
},
error:function(data){
$("#error").show().fadeOut(5000);
}
});
e.preventDefault();
return false;
});
});
looks like you're calling fadeOut on the div you are appending the the message to, so when it comes back round that div is still hidden when it appends the text
also removing the div from the dom after the fadeOut will make sure that there is only ever one of those divs in the dom at one time
$('<div id=\"test\" align=\"center\" style=\"padding-top:10px\"><p style=\"font-weight:bold;font-size:14px\">Your question has been posted.</p></div>').appendTo('#product_question');
setTimeout(function() {
$('#test').fadeOut('fast').remove();
}, 1000);
Looks like the problem is that the second time it cant apply because your '#product_question' is hidden. Or maybe you have multiple '#product_question' by the time user posts the second question? Maybe you should try using a class 'product_question' instead of id? '.product_question' vs '#product_question' is your styles.
Mate, I guess you're using way too much code in there. Have a hidden div as:
<div id="test" class="myclass" align="center" style="padding-top:10px; display:none;"><p style="font-weight:bold;font-size:14px;">Your question has been posted.</p></div>
and then show it on your call back:
if($.trim(data) == "success") {
$('.myclass').css('display','block');
setTimeout(function() {
$('.myclass').fadeOut('fast');
}, 1000);
}
This piece of code (with another php file) grabs some content from a database and pus it into a div called about_content.
$(document).ready(function(){
$('a#about-menu').click(function() {
var id = $('a#about-menu').attr('class');
$.post('subpages/content_about/about_content.php',{id: id}, function(id){
$('div#about_content').text(id)
});
});
});
Everything works, but can you tell me how I can modify this so the stuff fades in, instead of just being smacked right in... I'm not sure how to use the fadeIn function in this scenario..
Try..
$('div#about_content').css('opacity', '0').text(id).fadeIn();
you can do
$('div#about_content').hide();
$('div#about_content').html(id);
$('div#about_content').fadeIn(1000);
didnt test it, so im not sure you could try
This fades out the existing content then replaces the content and fade it in!
$(document).ready(function(){
$('a#about-menu').click(function() {
var id = $('a#about-menu').attr('class');
$.post('subpages/content_about/about_content.php',{id: id}, function(id){
$('div#about_content').fadeOut('fast',function(){ $(this).html(id).fadeIn(); });
});
});
});
DEMO
Hi everyone I have been working on this particular problem for ages by now,plz help.
I have looked at jQuery: Refresh div after another jquery action?
and it does exactly what I want but only once! I have a table generated from db and when I click on delete it deletes the row and refreshes the div but after which none of my jquery functions will work.
$('#docs td.delete').click(function() {
$("#docs tr.itemDetail").hide();
var i = $(this).parent().attr('id');
$.ajax({
url: "<?php echo site_url('kt_docs/deleteDoc'); ?>",
type: 'POST',
data: 'id=' + i,
success: function(data) {
$("#docs tr.itemDetail").hide();
$("#f1").html(data); // wont work twice
//$("#docs").load(location.href+" #docs>*"); //works once as well
}
});
});
in my body I have
<fieldset class='step' id='f1'>
<?php $this->load->view('profile/docs_table'); ?>
</fieldset>
profile/docs reads data from db. <table id='docs'>....</table>
and my controller:
function deleteDoc() {
$id = $_POST['id'];
$this->load->model('documents_model');
$del = $this->documents_model->deleteDocument($id);
return $this->load->view('docs_table');
}
Thanks in advance!
Are you removing any expressions matching $('#docs td.delete') anywhere? If so, consider using $.live(), which will attach your function to ALL matching elements regardless of current or in the future; e.g.
$('#docs td.delete').live('click', function() {
// Do stuff.
});
http://api.jquery.com/live/
Try using bind() instead of click(). The click() method won't work on dynamically added elements to the DOM, which is probably why it only works the first time and not after you re-populate it with your updated content.
You should just have to replace
$('#docs td.delete').click(function() {
with
$('#docs td.delete').bind('click', function() {
Are you replacing the html elements that have the events on them with the data your getting through ajax? If you end up replacing the td.delete elements, then the new ones won't automatically get the binding.
I want to basically create a link which says:
Click here to show contact information
Upon clicking it, it will ping a script via an ajax request, the ajax request will look up the user table where the ID is what is contained in the alt tag, it will return a certain field from the database and then the div will change from this link, to a contact number.
I'm sure some of you have seen this done before, for example:
Click to see persons phone number
They click it, and it changes to their phone number.
How would I go about doing this? I want to do it using ajax instead of having the phone number in the source code, because that really defeats the purpose of them having to click to reveal if bots can get it from the source code.
Thanks :)
Somethign along the lines of
$("#reveal").click(function(){
$.get('getphoneNumber.php',{id:$(this).attr('alt')}, function(data) {
$('#reveal').html(data);
});
});
with a php script called getphoneNumber.php that accepts a get parameter of id
Try this one
$('#reveal').click(function () {
var th = $(this);
$.get('/get-the-phone-number', { id: th.attr('alt') }, function (response) {
th.text(response);
});
});
Also, I'd recommend you put the id number inside a data-contact-id attribute and access it via th.data('contact-id') instead of using the alt attribute. Ignore me if you have other reasons to do this.
$("#reveal").live('click',function(event) {
var link = $(this).attr('alt');
var dataString = 'alt=' + link ;
$.ajax({
type: "POST",
url: "url",
cache:false,
data: dataString,
success: function(data){
this.href = this.href.replace(data);
}
});
}
Click here to show contact information
<div id="myHiddenDiv"></div>
$("#reveal").click(function(){
$.get("test.php", { id: $(this).attr("alt") },
function(data){
$("#myHiddenDiv").html(data);
$("#myHiddenDiv").show();
});
});
This example works assuming you've only got one of these "plugins" on your site, if you'll have multiple, use this:
Click here to show contact information
<div class="myHiddenDiv"></div>
$(".reveal").click(function(){
var divSelector = $(this).next("div");
$.get("test.php", { id: $(this).attr("alt") },
function(data){
divSelector.html(data);
divSelector.show();
});
});
I have code as follows:
$("#item_select").change(function()
{
var params = $("#item_select option:selected").val();
$.post('/account/ar_form.php', {idata: params}, function(data){
$("#message_display" ).html(data);
});
});
This is a dropdown that uses /account/ar_form.php to display html in the div correctly.
But it only displays on the change event. I'd like it to preload the data. When I use a load event, it will display the html, but on change, it displays it twice.
$("#item_select").change(function(){
var params = $("#item_select option:selected").val();
$.post('/account/ar_form.php', {idata: params}, function(data){
$("#message_display" ).html(data);
});
}).triggerHandler("change");
It depends a bit... what does your ar_form.php return with blank params?
You could do a hackish way (at fear of being downvoted) by calling
$("#item_select").change();