Loading a PHP file/code via Javascript.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon

    Loading a PHP file/code via Javascript.

    Hi,

    Sorry for the cross posting, but I think it applies to both languages.

    As we all know, JavaScript is client side and php is server side, (the php
    code is 'allowed' to do stuff on the server that JavaScript cannot).
    The problem with php is that it timeout after a while, (and the user also
    has no clue as to what is going on for a long time).
    I need to run a script on the server that could take a very long time.

    So what I was thinking is mixing both JavaScript and PHP
    Something like,

    <script>
    var endvalue = 1000; /* some number that the server can calculate
    quickly */
    var i = 0
    while (i<=endvalue)
    {
    /**
    call a php file that will do some work
    somefunction.ph p?someNumber=i
    */
    }
    </script>

    That way the server does the work, while the client keeps it going.
    Ideally I would also get a return value/string from the php script.

    How could I achieve something like that?

    Many thanks in advance.

    Simon


  • d

    #2
    Re: Loading a PHP file/code via Javascript.

    "Simon" <spambucket@exa mple.com> wrote in message
    news:46re0iFcj4 evU1@individual .net...[color=blue]
    > Hi,
    >
    > Sorry for the cross posting, but I think it applies to both languages.
    >
    > As we all know, JavaScript is client side and php is server side, (the php
    > code is 'allowed' to do stuff on the server that JavaScript cannot).
    > The problem with php is that it timeout after a while, (and the user also
    > has no clue as to what is going on for a long time).
    > I need to run a script on the server that could take a very long time.
    >
    > So what I was thinking is mixing both JavaScript and PHP
    > Something like,
    >
    > <script>
    > var endvalue = 1000; /* some number that the server can calculate
    > quickly */
    > var i = 0
    > while (i<=endvalue)
    > {
    > /**
    > call a php file that will do some work
    > somefunction.ph p?someNumber=i
    > */
    > }
    > </script>
    >
    > That way the server does the work, while the client keeps it going.
    > Ideally I would also get a return value/string from the php script.
    >
    > How could I achieve something like that?
    >
    > Many thanks in advance.
    >
    > Simon[/color]

    I've done something similar to this myself :)

    I found the best way to do it is to have your lengthy PHP script running in
    a hidden iframe, occasionally spitting out chunks of javascript (in complete
    <script> tags), and flushing the output after each tag is written. The
    javascript can update a textual display, or a progress bar or whatever, on
    the document holding the iframe. It's really simple to use, and very
    effective.

    dave


    Comment

    • pyda001@ec.auckland.ac.nz

      #3
      Re: Loading a PHP file/code via Javascript.

      Another option is to use CURL. Write another php-page that takes a
      couple of parameters and pass the parameters to this page. CURL exists
      for a whole bunch of languages.

      Not that I've explored this myself, but I guess AJAX is a third option.
      Check out xmlHttpRequest and it's peer functions.

      Good luck!


      - Peder -

      Comment

      • Simon

        #4
        Re: Loading a PHP file/code via Javascript.

        [color=blue]
        >
        > I've done something similar to this myself :)
        >
        > I found the best way to do it is to have your lengthy PHP script running
        > in a hidden iframe, occasionally spitting out chunks of javascript (in
        > complete <script> tags), and flushing the output after each tag is
        > written. The javascript can update a textual display, or a progress bar
        > or whatever, on the document holding the iframe. It's really simple to
        > use, and very effective.
        >
        > dave[/color]

        Hum, would you have a small example I could try?

        'cause I am not sure I follow, if the php times-out after 30 seconds, (the
        default), then there is nothing it can do really.
        I need to divide the work in chunks of less that 30 seconds and somehow get
        the JavaScript to call each 'chunck' one at a time.

        So I would need JavaScript to open a php script, wait for the output and
        handle the output.

        Is it not possible to call another file on the server JavaScript and echo
        the output?

        I hope my explanation is clear.

        Simon


        Comment

        • d

          #5
          Re: Loading a PHP file/code via Javascript.

          "Simon" <spambucket@exa mple.com> wrote in message
          news:46rh13Fc93 80U1@individual .net...[color=blue]
          >[color=green]
          >>
          >> I've done something similar to this myself :)
          >>
          >> I found the best way to do it is to have your lengthy PHP script running
          >> in a hidden iframe, occasionally spitting out chunks of javascript (in
          >> complete <script> tags), and flushing the output after each tag is
          >> written. The javascript can update a textual display, or a progress bar
          >> or whatever, on the document holding the iframe. It's really simple to
          >> use, and very effective.
          >>
          >> dave[/color]
          >
          > Hum, would you have a small example I could try?[/color]

          ----------------------------------------------------------------------
          index.html:

          <html>
          <head><title>Ex ample</title></head>
          <body onload="documen t.getElementByI d('ifr').src='/script.php';">
          <iframe id="ifr" style="display: none;" src=""></iframe>
          <div id="output"></div>
          </body>
          </html>
          ----------------------------------------------------------------------

          ----------------------------------------------------------------------
          script.php:

          <html><head></head><body><?

          function out($msg) {
          echo '<script language="javas cript"
          type="text/javascript">par ent.document.ge tElementById("o utput").innerHT ML="'.$msg.'"; </script>';
          flush();
          }

          set_time_limit( 0);

          function long_ass_proces s($e) {
          sleep(1);
          }

          $massive_array= array_pad(array (), 200, array());

          foreach ($massive_array as $i=>$element) {
          out('Processing #'.$i);
          long_ass_proces s($element);
          }

          out('Done.');

          ?></body>
          </html>
          ----------------------------------------------------------------------

          (I've not tested it, btw)
          [color=blue]
          > 'cause I am not sure I follow, if the php times-out after 30 seconds, (the
          > default), then there is nothing it can do really.
          > I need to divide the work in chunks of less that 30 seconds and somehow
          > get the JavaScript to call each 'chunck' one at a time.[/color]

          use set_time_limit( 0) to disable the time-out.
          [color=blue]
          > So I would need JavaScript to open a php script, wait for the output and
          > handle the output.[/color]

          Not if you disable the time-out :)
          [color=blue]
          > Is it not possible to call another file on the server JavaScript and echo
          > the output?[/color]

          No need :)
          [color=blue]
          > I hope my explanation is clear.
          >
          > Simon[/color]

          I hope this helps!

          dave


          Comment

          • Simon

            #6
            Re: Loading a PHP file/code via Javascript.

            >[color=blue]
            > set_time_limit( 0);[/color]

            I am running php in safe mode and I cannot change that.
            [color=blue]
            >
            > use set_time_limit( 0) to disable the time-out.[/color]

            Ah, but I cannot do that unfortunately.

            Thanks for that, but I cannot change the timeout stuff.

            Simon


            Comment

            • d

              #7
              Re: Loading a PHP file/code via Javascript.

              "Simon" <spambucket@exa mple.com> wrote in message
              news:46rl87Fcll 4pU1@individual .net...[color=blue][color=green]
              > >
              >> set_time_limit( 0);[/color]
              >
              > I am running php in safe mode and I cannot change that.
              >[color=green]
              >>
              >> use set_time_limit( 0) to disable the time-out.[/color]
              >
              > Ah, but I cannot do that unfortunately.
              >
              > Thanks for that, but I cannot change the timeout stuff.[/color]

              No problem ;)
              [color=blue]
              > Simon[/color]

              What restrictions do you have under your specific safe mode?


              Comment

              • Thomas 'PointedEars' Lahn

                #8
                Re: Loading a PHP file/code via Javascript.

                d <d@example.co m> wrote:
                ^^^^^^^^^^^[color=blue]
                > "Simon" <spambucket@exa mple.com> wrote [...][/color]
                ^^^^^^^^^^^
                I am curious: Do your service providers already know about your
                ongoing domain abuse? Does IANA, the owner of the domain?

                And will you ever stop crossposting off-topic without Followup-To?


                X-Post & F'Up2 news.admin.net-abuse.misc

                PointedEars

                Comment

                • d

                  #9
                  Re: Loading a PHP file/code via Javascript.

                  "Thomas 'PointedEars' Lahn" <PointedEars@we b.de> wrote in message
                  news:5774309.bp 8jj66Spa@Pointe dEars.de...[color=blue]
                  >d <d@example.co m> wrote:
                  > ^^^^^^^^^^^[color=green]
                  >> "Simon" <spambucket@exa mple.com> wrote [...][/color]
                  > ^^^^^^^^^^^
                  > I am curious: Do your service providers already know about your
                  > ongoing domain abuse? Does IANA, the owner of the domain?
                  >
                  > And will you ever stop crossposting off-topic without Followup-To?[/color]

                  And if you read RFC 2606 section 3, you will see that example.com is not
                  available for use, so no-one's being abused.
                  [color=blue]
                  >
                  > X-Post & F'Up2 news.admin.net-abuse.misc
                  >
                  > PointedEars[/color]


                  Comment

                  • NC

                    #10
                    Re: Loading a PHP file/code via Javascript.

                    Simon wrote:[color=blue]
                    >
                    > As we all know, JavaScript is client side and php is server side, (the php
                    > code is 'allowed' to do stuff on the server that JavaScript cannot).
                    > The problem with php is that it timeout after a while, (and the user also
                    > has no clue as to what is going on for a long time).
                    > I need to run a script on the server that could take a very long time.[/color]

                    Does the user need to see anything based on the results of this long
                    process?
                    [color=blue]
                    > How could I achieve something like that?[/color]

                    There are two options I can think of; neither involves JavaScript. I
                    am sure there are many other options, too.

                    1. Use command-line scripting

                    If there is no need for the user to see the output, you can start your
                    processing script in the background using the command-line interpreter.
                    On Unix, this will look something like this:

                    exec('php my_very_long_sc ript.php &');

                    2. Use "piecemeal" processing

                    Say, you need to process an unknown number of records in a database.
                    You estimated that processing 100 records at a time is done quickly
                    enough not to trigger the execution time limit. So you can repeatedly
                    run the same script to process a new group of records each time;
                    something like this:

                    if (isset($_GET['last_processed '])) {
                    $last = (int) $_GET['last_processed '];
                    $query = "SELECT * FROM mytable WHERE id>$last ORDER BY id LIMIT
                    100";
                    } else {
                    $query = "SELECT * FROM mytable ORDER BY id LIMIT 100";
                    }
                    $result = mysql_query($qu ery);
                    while ($record = mysql_fetch_arr ay($result)) {
                    // Process your records
                    $last_processed = $record['id'];
                    }
                    if (mysql_num_rows ($result) == 100) {
                    header('Locatio n: ' . $_SERVER['PHP_SELF'] .
                    "?last_processe d=$last_process ed");
                    echo "$last_processe d records processed so far... Please wait...";
                    die();
                    }
                    echo "Finished processing; $last_processed records were processed.";

                    Cheers,
                    NC

                    Comment

                    • google@impliedbydesign.com

                      #11
                      Re: Loading a PHP file/code via Javascript.


                      Simon wrote:[color=blue]
                      > As we all know, JavaScript is client side and php is server side, (the php
                      > code is 'allowed' to do stuff on the server that JavaScript cannot).
                      > The problem with php is that it timeout after a while, (and the user also
                      > has no clue as to what is going on for a long time).
                      > I need to run a script on the server that could take a very long time.
                      > That way the server does the work, while the client keeps it going.
                      > Ideally I would also get a return value/string from the php script.
                      > How could I achieve something like that?[/color]

                      Hello Simon,

                      I've put together a free, very simple AJAX tool that you can use to
                      accomplish the client / server information transfer:

                      Super AJAX Programming Seed


                      Chris S.

                      Free Web Design Tools


                      Comment

                      Working...