Solved

Jquery and Ajax - go getting all the information

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 …

777 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