Update web page from PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chrisS
    New Member
    • Oct 2006
    • 6

    Update web page from PHP

    I have been trying to find a way to update the current web page with data from a PHP program - without success. (searched this forum without success) Looking for some help now.

    To explain.. I have a page that calls a php program that will take quite a while to complete, so I want to display on the page the progress of the php. ie Status: Part One.. then when thats done CHANGE 'Part One' to 'Part Two' etc. I do NOT want to keep adding lines to the web page, I want to ALTER the page.

    I hope you understand what I mean and can help. I'm thinking that the solution will be a mix of Javascript and PHP.. I'm fairly new to both.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    If you want to update your page continually from the server, you'll need Ajax. If you just want to do it at the client-side you'll need JS to do that. These is a JS+CSS+DHTML sample (plus source) of a loading bar what you could change to your wishes at progress bar

    Ronald :cool:

    Comment

    • chrisS
      New Member
      • Oct 2006
      • 6

      #3
      Ronald:

      Thanks for the link.. This though raises a problem .. It says :
      :The moveProgressBar () function is called whenever you want to move the progress bar
      As I am running a php program, it is from there that I need to call the function, which naturally is in the main page on the site. I have been searching for 'how to call a javascript function from a PHP script' ... without success.. Then if this is possible why can I not set a var or session var in the php and pass that through a function ? Which would of cause answer my query and solve the problem.

      Everywhere I go I seem to get a half answer. This tells me to call a function but fails to tell me how to do it. Bit like saying 'If you want to go to the moon build a rocket' Or am I missing something here?

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        Since you are running the PHP program and that program updates the HTML page at the client, it is all up to you when to display the bar and with what intervals. I don't see the problem. But I have found a couple of PHP classes at phpclasses.org. One of them must satisfy the requirement. They are:

        Progress Bar
        ProgressBar (not the same as above)
        PHProgress Bar

        Good luck!

        Ronald :cool:

        Comment

        • chrisS
          New Member
          • Oct 2006
          • 6

          #5
          Since you are running the PHP program and that program updates the HTML page at the client, it is all up to you when to display the bar and with what intervals. I don't see the problem.
          It doesn't/can't - that is the problem..

          to further explain:
          User visits site and fills in a form then clicks submit.
          form calls the php program which moves files/sets up config files/sets up directories etc: (this takes about 2-3 minutes).

          Now I can easilly echo the progress to the browser using echo and flush. Which produces a new page with:-
          moving files [run a bit of php]
          creating myqsl database [does that]
          and so on.

          What I want to do is have on the existing page a line/box with
          Progress: <some sort of var in here showing the progress the php>. As I'm sure you know if I echo anything from the php it blanks the screen. If I refresh the browser (to re-read a $_SESSION var) it escapes from the php program which terminates.

          Hence I thought the solution would need to be a JS/PHP mix . As I said I am fairly new to this and perhaps attempting something too complex. Perhaps I should just settle for a blank screen and echo to it even though that looks hidious

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            To me that sounds like the perfect Ajax solution to me! I am not a very good Ajax developer (I've only made some small applications), but I am msure this is Ajax.

            First, I include a link I found to an article about Ajax file upload progress bar. Look at it.
            Secondly, I'll send a note to the moderator of the Ajax/JS forum and ask him to join this conversation. Until then.

            Ronald :cool:

            Comment

            • iam_clint
              Recognized Expert Top Contributor
              • Jul 2006
              • 1207

              #7
              Hey guys

              I can't think of a way to call a javascript function from php but YOU CAN pass variables from PHP to Javascript.


              Now you could do this with ajax but lets try to come up with a less complicated script.

              Code:
              <?
              $progress = "20%"
              ?>
              <script>
              window.onload = updateprogress();
              function updateprogress() {
              var tmp = "<?=$progress?>";
              document.getElementById('progress').innerHTML = tmp+" Completed"
              }
              </script>
              <html>
              <body>
              <div id="progress"></div>
              </body>
              </html>


              now you could simply do this by the echoing you also said but take it alittle further than that how about this.
              Make an inline frame have the php run in this inline frame use this code in the inline frame and have it update the real page with the current progress while having the inline frame hidden. document.parent ...

              Comment

              • chrisS
                New Member
                • Oct 2006
                • 6

                #8
                That could be an option, I thought of another solution, which may be of interest to others watching this thread.

                Before I explain may I thank you for your pointers and patience.

                As I said, the setup.php is called from a form (and I want a var on that page updating)..I have decided to
                a) split up the setup.php into the 6 parts that it does
                b) design a template page (same as the index page).
                c) Used a frameset with a zero top frame (just to hide the urls as progs call progs). Even though I'm not a great fan of frames.

                my tmpplate.php has in it..
                [html]
                <td><?php echo $_SESSION['progress'] ?></td>
                <!-- and the form fields are ... -->
                <td><input name="name" type="text" id="name" value="<?php echo $_SESSION['actName'] ?>" maxlength="30"> </td>
                <!--
                $_SESSION['actName'] is created in the first part of the php (start.php)
                this means that when the refresh occures the data the user put it is 'replaced'
                Hope you follow what I mean. I can put up a demo site if you would like, but you seem very intelligent
                -->
                [/html]

                Now the form calls start.php.

                start.php is
                [php]
                <?php
                session_start() ;
                //phpcode .which grabs the form data and passes them into $_SESSION... ending with
                $_SESSION['progress']="Creating Folders...";
                echo "<META http-equiv='refresh' content='3;URL= http://www.theurl/scripts/part1.php'>";
                include ('tmpplate.php' );
                ?>
                [/php]

                part1.php is
                [php]
                <?php
                session_start() ;
                // this will check and setup folders
                // it is called from start.php which said 'Creating Folders'
                /// all php code in here
                /// to set up the folders
                // prep and call part 2
                $_SESSION['progress']="Creating Files...";
                echo "<META http-equiv='refresh' content='3;URL= http://www.theurl/scripts/part2.php'>";

                include ('tmpplate.php' );
                ?>
                [/php]

                and so on.. this I know is somewhat messy, but it has the deception of updating the var, and any vars I need throughout the original(now split-up) program are also to be stored in $_SESSION . There is no real sensitive information being stored so I'm not too concerned with storing the data in $_SESSION. part 6 destroys the session and returns to the original index.

                Does this make sence ? Have we achieved the impossible?
                A new approach to modular programming perhaps :)
                Again thank for your patience, guidence, and may I say speed of responce with a newby to php

                Comment

                • chrisS
                  New Member
                  • Oct 2006
                  • 6

                  #9
                  Ronald, I did look at that AJAX site (Even bookmarked it), also those links to other sites and I thank you for that.

                  A little background on me, about 20 years ago I was a fairly good (for those times) programmer. However, I got old and technology moved on - passed me by - I know a little bit of a lot of languages (some now dead - or is MASM still used? ). I have rarely and barely programmed for around 7 years. I was asked about a week ago to come up with a solution, by someone I knew from the past (I had a reputation for achieving what was 'not possible') - he sort of challanged me, 6 web design companies he approached told him it could not be done. I had a hunch that php could get into corners that PERL could not. The guts of the program I had done.. all the setting up/moving files/setting up MsSQL databases etc - except this.. I was completely vexed with this problem. With your help and guidence we have achieved a potential solution. Thank you so much.

                  I will take a look at that AJAX site at some point, but first I need to try to get to understand PHP a little better. (luckily I am verced in perl - to a degree) and the logic of PHP seems like a cross between C and PERL.. It actually seems to be a nice clean language -

                  But then I am retired from programming now. This stuff does need to be in young brains - us oldies need to let go (just play once in a while - like grandad on the swings in the park)... That's called progress, and long may it go on for without it we would still be naked in caves.

                  I am very pleased to see that bbs's (forums now) are still such a good source of skilled people, that take the time to help others. I am so greatful for the curtosy and patience shown me on this forum.

                  Thank you.

                  Comment

                  • chrisS
                    New Member
                    • Oct 2006
                    • 6

                    #10
                    Iam,

                    Did I not read somewhere that some browsers don't accept/recognise iframes?? would that cause a problem?

                    Actually I might have read that years ago - just something in the back of my mind about iframes...

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Keep up the spirit!!! As an experienced programmer, >37 years, I know it looks all more superficial nowadays, but don't let it get you down! Things like PHP and Ajax, it's just new wine in old bags, the same as it was 40 years ago!! The language may be changed, but the basics are still the same as they have always been.

                      And indeed, PHP is a child of Perl (maybe C, but that was only 1973), so it isn't that hard to comprehend. Programming isn't that hard, only the language changes, not the basics.

                      As they say: "old programmers never die".

                      Ronald :cool:

                      Comment

                      Working...