Solved

Jquery and Ajax - go getting all the information

Posted on 2014-07-20
5
413 Views
Last Modified: 2014-07-20
Hi E's, I have a script that made a call in Ajax (jQuery) for get data/information from a php file, like this:
            $("#optimizar_responsivo").click(function(){
                $.ajax({
                    type: "GET",
                    url: "optimizar_responsivo.php?id=<? echo $_GET['id']; ?>",
                    success: function(dat){
                        if(dat == "sete"){
                            var actual = "imagem_edita.php?id=<? echo $_GET['id']; ?>&erro=Contacte o Suporte Tecnico do Painel de Controlo";
                            window.location=actual; 
                        } else if(dat == "sucesso"){
                            var actual = "imagem_edita.php?id=<? echo $_GET['id']; ?>&restaurar=1";
                            window.location=actual; 
                        }else{
                            alert(dat);
                            var actual = "imagem_edita.php?id=<? echo $_GET['id']; ?>&erro=Não foi possivel fazer uma optimização da imagem - É aconselhavel remover esta imagem do sistema";
                            window.location=actual;     
                        }
                    }
                });
            });

Open in new window

The code above is correct, the problem is, they just be executed once!
My objective is create a progress bar, and in php file, I write some php "echo" like: echo "5"; in the beginning of the code, echo "50"; in the middle of the code, and so on, to reach 100.
Is possible change my code for every time php file show a "echo" execute this part of the code:
if(dat == "sete"){
                            var actual = "imagem_edita.php?id=<? echo $_GET['id']; ?>&erro=Contacte o Suporte Tecnico do Painel de Controlo";
                            window.location=actual; 
                        } else if(dat == "sucesso"){
                            var actual = "imagem_edita.php?id=<? echo $_GET['id']; ?>&restaurar=1";
                            window.location=actual; 
                        }else{
                            alert(dat);
                            var actual = "imagem_edita.php?id=<? echo $_GET['id']; ?>&erro=Não foi possivel fazer uma optimização da imagem - É aconselhavel remover esta imagem do sistema";
                            window.location=actual;     
                        }

Open in new window

The best regards, JC
0
Comment
Question by:Pedro Chagas
  • 3
  • 2
5 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40207566
You can't really do that as you would need to know how long the process would take.
Ajax by default is asynchronous so as soon as you execute it your page will carry on doing what it's doing until it receives a response from the server.

You could use web sockets but that is a whole different story
http://code.tutsplus.com/tutorials/start-using-html5-websockets-today--net-13270
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 40207569
You mean from AJAX is impossible?
~JC
0
 
LVL 58

Expert Comment

by:Gary
ID: 40207571
With ajax there is no two way communication, the request is sent and then a listener waits for the response and closes the connection
Look at websockets (above) if you want real time communication with a server
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 40207585
Ok thanks. To finalize, I not will use websockets, I thing I have a great idea and would like to know your opinion about it:
Because the php file, in average takes 10 seconds to process, I thing, in php file change "echo" for database update, and in my html file run a javascript script that verify each 1 second if have changes in data base.
It is a good idea?
~JC
0
 
LVL 58

Expert Comment

by:Gary
ID: 40207589
If I am understanding you correctly that might work.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now