Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Jquery and Ajax - go getting all the information

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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

839 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