Solved

Jquery and Ajax - go getting all the information

Posted on 2014-07-20
5
418 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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