I have a script called notify_me.php which I run like so:
script type="text/javascript">
//Run notify_me.php script
$(document).ready(function(){
$("#submit").click(function(){
var d = $('form').serialize();
$.get('php/notify_me.php',d,function(data){
$(".container").html(data);
});
});
});
</script>
and I would like to show a animated loading .gif until the script is printed. The code for that is :
<div class="search_load" >
<span class="searching_font"> Fetching your flight prices...</span><br/><br/>
<img src="images/searchloading.gif">
</div>
How can I write a function that shows the .search_load class during the processing of notify_me.php. The .search_load class should also be shown in the .container div.
Tried some different ways with append(); and appendTo(); but can't seem to get it working.
Appreciate your help on this!
EDIT: I got the .search_load div to show the first time I hit the #submit. The second time it doesn't show att all. This is the problem I'm trying to solve. Any ideas?!
SOLUTION: Guys, I solved it by looking in to #Rohan's link. Check out below. Thank you all!
$(document).ready(function(){
$("#submit").click(function(){
$('#info').show();
$('html,body').animate({scrollTop: $("#info").offset().top},'slow');
$('.container').after('<div class="search_load"></div>');
var d = $('form').serialize();
$.get('php/notify_me.php',d,function(data){
$(".container").html(data);
$('.search_load').hide();
});
});
});
$(document).ready(function(){
$('.search_load').hide();
$("#submit").click(function(){
var d = $('form').serialize();
$('.search_load').show();
$.get('php/notify_me.php',d,function(data){
$('.search_load').hide();
$(".container").html(data);
});
});
});
You can also hide the div with CSS, so you can remove the line #2: $('.search_load').hide();
Steps:
Make your loading div hidden
When loading starts, display it using .show() and modifying the CSS so that its placement is correct
OnLoaded -> div.hide()
Refer to this: How to show waiting message during sync ajax call in browser
jQuery(document).ready(function($) {
$("#submit").click(function(){
$(".search_load").show();
var d = $('form').serialize();
$.get('php/notify_me.php', d, function(data) {
$(".container").html(data);
$(".search_load").hide();
});
});
});
try this:
$('div.search_load').hide();
$('#submit').click(function() {
$('div.search_load').show();
var d = $('form').serialize();
$.get('form.php', d,
function(data) {
$('div.search_load').hide();
$('.container').html(data);
},
'html');
return false;
});
Try This:
Javascript:
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
var d = $('form').serialize();
$(".search_load").show();
$.get('php/notify_me.php',d,function(data){
$(".container").html(data);
$(".search_load").hide();
});
});
});
</script>
HTML Changes:
<div class="search_load" style="display:none" >
<span class="searching_font"> Fetching your flight prices...</span><br/><br/>
<img src="images/searchloading.gif">
</div>
Hope this will work.
Related
I have following script which fetch data from server based on form parameters.
jQuery('#request_search').submit(function(e){
e.preventDefault();
var s_date=jQuery('#actualfrom').val();
var e_date=jQuery('#actualto').val();
var type=jQuery("#type").val();
var loc=jQuery("#loc").val();
jQuery.post("scripts/get_gamma_activity_type.php", {"loc":loc,"start_date":s_date, "end_date":e_date, "type":type}, function(data) {
jQuery('#report').html(data);});
});
});
This part is working and some times, based on search criteria , it takes a couple of seconds to get results. in the mean time I would like to show some GIF saying data loading. I have the GIF ready. How to implement it in the above script?
please try this.
jQuery('#request_search').submit(function(e){
$("#report").html("<div><img src='loading.gif' alt='Loading.. \n Please wait' title='Loading.. \n Please wait'` /></div>");
e.preventDefault();
var s_date=jQuery('#actualfrom').val();
var e_date=jQuery('#actualto').val();
var type=jQuery("#type").val();
var loc=jQuery("#loc").val();
jQuery.post("scripts/get_gamma_activity_type.php", {"loc":loc,"start_date":s_date, "end_date":e_date, "type":type}, function(data) {
jQuery('#report').html(data);});
});
});
Add Image with path in your html file with id attribute and then show/hide div
based on request and success.
Try below code:
Add html div in your html file :
<div id='loadingmessage' style='display:none'>
<img src='loadinggraphic.gif'/>
</div>
Try below code of jQuery:
jQuery('#request_search').submit(function(e){
$('#loadingmessage').show(); // show the loading message.
e.preventDefault();
var s_date=jQuery('#actualfrom').val();
var e_date=jQuery('#actualto').val();
var type=jQuery("#type").val();
var loc=jQuery("#loc").val();
jQuery.post("scripts/get_gamma_activity_type.php", {"loc":loc,"start_date":s_date, "end_date":e_date, "type":type}, function(data) {
jQuery('#report').html(data);});
$('#loadingmessage').hide(); // hide the loading message
});
});
I am currently using a very similar AJAX post request across many parts of my page:
$(document).ready(function() {
$("#name").change(function(e){
var vname = $("#name").val();
$.post("addit.php", {name:vname}, function(response, status) {
$("#table").html(response);
});
});
});
The above code works perfectly.
I am having a problem getting any functionality with dynamically loaded content. So for example a form grabbed by an AJAX call and put into my page this above does not work.
If we can pretend that I was running the same AJAX call as above but on dynamically loaded content from a PHP script using AJAX. what would my call look like. #table is a static element that is always present on the page.
I have tried this but it is not working:
$(document).ready(function() {
$("#table").on("click", "#btn1", function(e){
e.preventDefault();
var vname = $("#name").val();
$.post("addit.php", {name:vname}, function(response, status) {
$("#table").html(response);
});
});
});
Currently I am getting nothing show up on console and it just does not work.
Is what I am doing here correct?
The html would look like this: echoed from php:
<table id='table'>//this is the static element form is echoed
<form method='post'>
<input id='name' name = 'name'>
<button id='btn1' type='submit'>Add me</button>
</form>
</table>
I have changed my code slightly click on button rather than on change.
Try updating the code with following
$(document).ready(function() {
$("#table #btn1").on("click", function(e){
e.preventDefault();
var vname = $("#name").val();
$.post("addit.php", {name:vname}, function(response, status) {
$("#table").html(response);
});
});
});
It is not possible to set a function on a dynamically added element, when the element is not there in document
And instead of <table> use <div> if possible
I think your problem is that you miss
$( document ).ajaxComplete(function() { "script here will run and be available after ajax load" });
I have a link that opens a modal when clicked, however that element also has a data-id="#id here" attribute that I need to grab so I can pass it to my php script for processing. How can I accomplish this?
My JS code:
$(document).ready(function() {
$('#edit_general').click(editModal);
});
function editModal(event)
{
var modal = $('#editModal');
modal.modal({
show: true
});
}
My html:
<li><a id="edit_general" href="#editModal" data-uid="<?php echo $u->id; ?>">Edit General</a></li>
I tried using the this keyword but it didn't work.
How can I accomplish this?
Try this: Demo http://jsfiddle.net/szS2J/1/ or http://jsfiddle.net/ug7ze/
$(this).data('uid')
Hope it fits the cause :)
like this:
$('#edit_general').click(editModal);
$(this).data('uid')
full
$(document).ready(function() {
$('#edit_general').click(editModal);
});
function editModal(event) {
alert($(this).data('uid'));
var modal = $('#editModal');
modal.modal({
show: true
});
}
You can get the value using the attr method: $(this).attr('data-uid')
Like this stored in data_id.
$(document).ready(function() {
$('#edit_general').click(function() {
var data_id = $(this).data('uid');
var modal = $('#editModal');
modal.modal({
show: true
});
});
});
I'm not sure why i can' t get my output to surface. It shouldn't be too hard but has been stuck for several hours. All i
<script>
$(document).ready(function(){
$('#chat_submit').click(function(){
var data="{'chat_content:'+$('#chat_content').val()+'chat_from_who:'+$('#chat_from_who').val()+'chat_to_who:'+$('#chat_from_who').val()+'order_no:'+$('#order_no').val()}";
$('#order_chatbox').load('/seller/helpers/order_chat.php',data);
});
});
</script>
Any help here will be greatly appreciated.
You are sending the data as a long string.. not as an object.
Change it to
var data= {'chat_content': $('#chat_content').val(),
'chat_from_who': $('#chat_from_who').val(),
'chat_to_who': $('#chat_from_who').val(),
'order_no': $('#order_no').val()};
Try the following:
$(document).ready(function() {
$('#chat_submit').click(function(event) {
// event.preventDefault();
var data = {
chat_content: $('#chat_content').val(),
chat_from_who: $('#chat_from_who').val(),
chat_to_who: $('#chat_from_who').val(),
order_no: $('#order_no').val()
};
$('#order_chatbox').load('/seller/helpers/order_chat.php', data);
});
});
As a beginner in jQuery I'm trying to understand how to handle .click().
My event click function :
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('.del').click(function() {
var lien = $(this).attr("title");
$.post(lien, function(data) {
$('#result').empty();
$('#result').append(data);
})
});
</script>
The span :
<span title="<?php echo base_url().'del/cat/'.$cat->idcategories ?>"class="del alignright">supprimer</span>
This works the first time but not after that. Is this happening because I'm using attr()?
Any explanation of the expected behavior would be very much appreciated.
First of all, it looks like you're missing the last bracket-parens, to close document.ready. This could (should) be causing an error. Your code should be like this:
$(document).ready(function() {
$('.del').click(function() {
var lien = $(this).attr("title");
$.post(lien, function(data) {
$('#result').empty();
$('#result').append(data);
});
});
});
For good measure, also add a space before class in your span:
<span title="<?php echo base_url().'del/cat/'.$cat->idcategories ?>" class="del alignright">supprimer</span>
Then consider the other answers of changing click to live in the event that you're somehow replacing or unbinding .del elsewhere.
By the looks of it you sending a request to delete something... If your sending the request twice it's only going to delete once..
However Try
$('.del').live('click',function(){
var lien = $(this).attr("title");
$.post(lien, function(data) {
$('#result').empty();
$('#result').append(data);
}
});