Solved

Jquery and Ajax - go getting all the information

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
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.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

930 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

10 Experts available now in Live!

Get 1:1 Help Now