I have developed application for analysis data ie. domain name. When user provide 10 domains the following javascript code working fine but when user start analysis for 100 domains, below code does not work. I used javascript to redirect to another page after 3 second of form submit because processing assign task takes at least 1 minute time.
function submitForm(){
document.form1.button2.click();
var t=setTimeout("redir()",3000);
}
function redir(){
window.location.href = '<?php echo base_url();?>menu/showmsg';
}
When it is small task it is working fine but if there is big file to process javascript does not work, it wait till task completed from PHP side.
Is there any option in AJAX or JQUERY or any finest code in JavaScript?
A simple way is to use an iframe in the target page that runs the php. The page will load, and the iframe will wait for the php page. You can use some javascript to check for a change or flag in the iframe to move on with the process.
You can sent all requests asynchronously using for example jQuery.ajax( url [, settings] ).
Here the documentation: http://api.jquery.com/jQuery.ajax/
$.ajax({
url: '<?php echo base_url();?>menu/showmsg',
error: function(){
return true;
},
success: function(msg){
// make what do you want
}
});
Related
I make multiple AJAX calls on a web page,and they take a lot of time to complete (5 minutes). During those calls, if I try to go to another page on my website (with a new window or not), I must wait for the AJAX calls to finish before the page loads.
I make the calls asynchronously:
ajax_seek=$.ajax({
type: 'POST',
url: url_tracks,
data: { youtubes : tab_tracks },
sucess:function(a){
console.log(a);
}
});
Anybody have an explanation ?
So like CBroe said the answer was to put "session_write_close();" in my php file wich is called
with ajax. And it worked :)
i need a a script that will refresh the functions:
$ping, $ms
every 30 seconds, with a timer shown,
i basicly got this script:
window.onload=function(){
var timer = {
interval: null,
seconds: 30,
start: function () {
var self = this,
el = document.getElementById('time-to-update');
el.innerText = this.seconds;
this.interval = setInterval(function () {
self.seconds--;
if (self.seconds == 0)
window.location.reload();
el.innerText = self.seconds;
}, 1000);
},
stop: function () {
window.clearInterval(this.interval)
}
}
timer.start();
}
but it refreshes the whole page, not the functions i want it to refresh, so, any help will be appriciated, thanks!
EDIT:
I forgot to mention that the script has to loop infinatly
This here reloads the whole page:
window.location.reload();
Now what you seem to want to do is reload portions of the page, those portions having been generated by php functions. Unfortunately php is server side so that means you cant get the client browser to run php. Your server runs the php to generate stuff that browsers can understand. In a web browser open a page you made using php and choose to view source and you'll see what I mean.
Here's what you'll need to do:
Make your two functions ping and ms accessable via ajax
Instead of window.location.reload() do a call to jQuery.ajax. on success write to your page
Here's what I think would be the ideal way of dealing with this... I haven't seen the php side of your problem but anyway:
make a file called ping.php and put all your ping function code in there. ditto for ms
in your original php file that called those functions, make a div at each point where you wanted a function call. Give them appropriate ids. Eg: "ping_contents" and "ms_contents"
You can populate these with some initial data if you want.
In your js put in something like this:
jQuery.ajax(
{
url : url_of_ping_function,
data : {anything you need},
type : 'POST', //or 'GET'
dataType: 'html',
success : function(data)
{
document.getElementById("ping_contents").innerHTML = data;
}
});
do another one for the other function
What you want is AJAX, Asynchronous JavaScript and XML
You can use jQuery for that.
I can put an example here, but there is a lot of information to be found on the internet. In the past I wrote my own AJAX code, but since I started using jQuery, it's all a lot easier. Look at the jQuery link I provided. There is some usefull information. This example code might be the easiest to explain.
$.ajax({
url: "test.php"
}).done(function() {
alert("done");
});
A some moment, for example on a click on a button, the file test.php is executed. When it's done, a alert box with the text "done" is shown. That's the basic.
I have a form which submits an AJAX request to one of my controllers which uploads a file using PHP's curl. I want to show the user the status of that (PHP) upload, so I store the PHP upload progress in a session variable. Meanwhile, the submission of the form also starts a setInterval() which makes a different AJAX request to controller which checks the session variable. My problem is that the second AJAX call seems to only fire once (instead of throughout the upload process) and so instead of progressively updating the progress, it just returns 100 at the end. What am I doing wrong?
Here's my code:
(note: I'm using the jQuery form plugin to assist with the file upload. It also adds some additional callbacks)
<script>
var check_progress = function() {
$.ajax(
{
url : '/media/upload_progress',
success : function(data) {
console.log(data);
},
async : false
}
);
var options = {
beforeSend : function() {
$("#MediaSubmitForm").hide();
$("#MediaSubmitForm").after('<img class="hula-hippo" src="/img/hippo-hula.gif" />');
t = setInterval( check_progress, 500 );
},
success : function(data){
$(".hula-hippo").hide();
$("#MediaSubmitForm").after("<h3>Upload complete!</h3><p>Do <strong>you</strong> want to <a href='#'>create a project</a> of your own?</p>");
window.clearInterval(t);
console.log(data);
}
};
$("#MediaSubmitForm").ajaxForm(options);
</script>
Use setTimeout();. setInterval() executes the code after the time specified and setTimeout() executes the code every time it reaches the specific time.
These question explain well of their difference :)
setTimeout or setInterval?
'setInterval' vs 'setTimeout'
setInterval & setTimeout?
setInterval and setTimeout
JavaScript setInterval and setTimeout
And a search of this on SO will solve your problem :)
It sounds like this is a PHP locking issue. See the first comment in the answer to this question:
jQuery: Making simultaneous ajax requests, is it possible?
I have some script that takes a form and sends it to php using jquery ajaxSubmit.
Server side must copy image from one location to another.
using this code copy($img_dir_file,$mini_dir_file); is ok if u have few images.
But if u have let's say 20+ images php works slowly, and responds to ajax before finishing it's job. Thus Ajax changes content to blank page , cause result is not ready.
If you refresh page a bit later, everything is ok cause php finishes his work.
So please tell me what should I do with this problem ?
script is something like this
$('#save_edited_article').live('click',function(){
$('#edited_article_form').ajaxSubmit({
success: function(responseimage){
$('#main_content').html(responseimage) } }); });
You could use a Promise which will inform the browser when the job is complete.
Check out the jqXHR Object as part of Ajax on JQuery - you could then change your main content when the jqxhr object ( which works as a Promise) is complete :
jqxhr.done(function(){ $('#main_content').html(responseimage); });
Edit
An example using your code could be:
$("#edited_article_form").submit(function() {
var jqxhr = $.post(
"foo.php",
$("#edited_article_form").serialize()
);
jqxhr.fail(function(){ alert("fail") });
jqxhr.done(function(responseimage){ $('#main_content').html(responseimage) });
});
Disclaimer : This was written on the fly and may not compile. It is for example purposes only
Have the AJAX call check to see if it's complete, if it's not, keep polling every few seconds until it's ready.
Updated jQuery and ajaxSubmit plugin and everything works nice.
I'm making a fairly simple rating system, and I've got a small problem. When you +1 rate something, I'm trying to run a PHP script which will connect to the database, download the value from it, +1 to that value, and UPDATE the value in the database again.
I don't think reloading the page for a continious rating system would be a very good idea :S
I'm wondering how I can toggle a PHP script with Ajax, so that when you Click an image of a + sign, it runs the PHP add 1 script, and the + button turns in to a tick. I'm crap at ajax, and I'd go for trying jQuery + $.ajax({}); but I've failed 73 attempts. haha.
Anyone willing to give me a hand writing an Ajax script? :DDD
Thanks! :)
If you want someone to click a link which will access your page, let's assume you have this marup:
<a class = 'plusOne' id = 'someIDForYourSQLTable'>+1</a>
The ID is what you are going to pass to your server script so you can update the appropriate row, generally speaking this should be a primary identifier (i.e. Key) for the record that you want to +1.
Here is the jQuery that will send the ajax request to the file: plusOne.php in the same directory as the current page:
$(function() {
$(".plusOne").bind("click", function() {
$.ajax({
type: "GET",
data: "v="+$(this).attr("id"),
url: "plusOne.php",
success: function(data) {
// Whatever you want to do after the PHP Script returns.
}
});
});
});
The request will send the a URL parameter 'v' which you can access in your PHP script from the $_GET super global array.
html
<img src="plusone.png" rel="some_unique_id" class="rate" />
javscript
$(".rate").click(function() {
var elem = $(this);
$.get('/rate.php?id=' + elem.attr('rel'), function() {
elem.attr('src', 'checked.png').unbind('click');
});
});
and in php
mysql_connect('localhost','db_user','pssword');
mysql_query('UPDATE database_name.table_name SET rating=rating+1 where id=' . mysql_real_escape_string($_GET['id']));
Have a look at xAjax, a library to expose PHP functions/method to client-side JavaScript. xAjax makes things very simple.
For example, you are able to perform several changes in the browser in parallel:
$objResponse = new xajaxResponse();
$objResponse->assign("myInput1","value",$DataFromDatabase);
$objResponse->assign("myInput1","style.color","red");
$objResponse->append("myDiv1","innerHTML",$DataFromDatabase2);
$objResponse->prepend("myDiv2","innerHTML",$DataFromDatabase3);
$objResponse->replace("myDiv3","innerHTML","xajax","<strong>xajax</strong>");
$objResponse->script("var x = prompt("Enter Your Name");");
return $objResponse;