Here is how to 'do PHP' without leaving a page. (+ Question)

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

    Here is how to 'do PHP' without leaving a page. (+ Question)

    A FAQ here goes something like "How can I get input from the middle of
    my form sent processed to PHP and the result returned to the page?"

    The standard answer is "You can't because PHP is server side and HTTP is
    the bridge between client and server and it works in whole pages at a
    time. (One request -> complete response)"

    But _it is possible_ to use PHP to validate form input as it is being
    inputted and update the page _without_ reloading the page.

    Here are two files, orville.htm and wilbur.php which demonstrate the
    method. (Copy both to same directory and access orville - wilbur is a
    slave.)

    There are limitations which may not make it suitable for all
    applications but nevertheless an interesting technique.

    Q: My ancient javascript skills limit me to hacking <input> values. Is
    there a way to access other document bits dynamically? For example to
    have a bit of plain text that said "The server replied at /some time/"
    (where 'some time' started off life as Date('H:i:s'); in PHP).


    =============== ===orville.htm= =============== ============
    <html>
    <head>
    <!-- Proof of concept for flying input (III) page 1 of 2
    secondary window/script is automatically opened
    -->

    <SCRIPT LANGUAGE="JavaS cript">
    <!--

    function Fly(){
    jt = 'wilbur.php?VAL ='+escape(docum ent.frm.bar.val ue) // escape makes
    string safe to add to URL
    window.open(jt,
    '','resizable=n o,toolbar=no,sc rollbars=no,sta tus=no,width=40 height=40')
    return true
    }

    //-->
    </SCRIPT>


    </head>
    <body>

    <h1>Flying screens for input validation</h1>
    <h2>Proof of concept</h2>
    <i>This operates in conjunction with wilbur.php</i>
    <p>
    <SCRIPT TYPE="text/javascript">
    <!--
    d = new Date()
    document.write( "<b>Page refreshed at "+d.toLocaleStr ing() + "</b><p>")
    // -->
    </SCRIPT>

    This represents some arbitary form<br>
    Type something interesting into Bar
    <p>

    <FORM NAME="frm">
    <table bgcolor="#b0b0b 0">
    <tr><td>Foo : </td><td><INPUT NAME="foo" SIZE=20
    VALUE="Foo"></td><tr>
    <tr><td>Bar : </td><td><INPUT NAME="bar" SIZE=20 VALUE="abc"
    onBlur="javascr ipt:Fly()";> (Automatically validated in
    PHP-land)</td></tr>
    <!-- alternative in-line version which means the <script...Funct ion
    Fly().../script> can be omitted
    Bar : <INPUT NAME="bar" SIZE=10 VALUE="pqr"
    onBlur="window. open('proofoc6. php?VAL='+escap e(document.frm. bar.value),
    '','resizable=n o,toolbar=no,sc rollbars=no,sta tus=no,width=40 ,height=40')";>

    --->
    <tr><td>Fox : </td><td><INPUT NAME="fox" SIZE=20
    VALUE="Fox"></td></tr>
    </table>
    </FORM>
    <p>
    What should happen is that the value in Bar gets reversed and Foo gets
    copied to Fox.<br>
    It may not if<br>
    (a)javascript is turned off or <br>
    (b)popup windows are blocked
    <p>
    Notice that the page does not get reloaded.<br>
    Other data in the form remains intact.<br>
    <p>
    The 'validation' of Bar is done by sending the value of Bar on the
    command line.
    This is the way to get values across to PHP.
    <p>
    Copying Foo to Fox is pure javascript which doesn't need to be executed
    in another window
    but proves that the flying window can access this window's variables for
    read and write.


    </body>
    </html>
    =============== =============== ===============

    =============== ===wilbur.php== =============== ===
    <?php

    // Proof of concept for flying input (III) 2 of 2
    // Of course the PHP here is trivial but
    // could be anything you want.

    // -------- in the world of PHP --------------
    // Uses command line to read current value
    $currentValue=$ _GET['VAL'];

    // do something with it

    if(!$currentVal ue){
    $valbar='(none) ';
    }else{
    $valbar = strrev($current Value);
    if($valbar==$cu rrentValue){$va lbar='Palindrom e';}
    }

    // --------- back to the world of Javascript ------
    ?>
    <html><head>
    <SCRIPT LANGUAGE="JavaS cript">
    <!--
    opener.document .frm.bar.value= '<?php print($valbar); ?>'
    x=opener.docume nt.frm.foo.valu e;
    opener.document .frm.fox.value= x;
    window.close();
    -->
    </SCRIPT>
    </head>
    <body>
    </body>
    </html>
    =============== =============== =============== =======


    --
    PETER FOX Not the same since the bookshop idea was shelved
    peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
    2 Tees Close, Witham, Essex.
    Gravity beer in Essex <http://www.eminent.dem on.co.uk>
  • C.

    #2
    Re: Here is how to 'do PHP' without leaving a page. (+ Question)

    There are existing RPC implementations that do this a lot more neatly.
    Ajax seems to be the prefered method these days - but its limited in
    backward compatability (while iframe based versions are limited in
    forward compatability).

    C.

    Comment

    • Chung Leong

      #3
      Re: Here is how to 'do PHP' without leaving a page. (+ Question)


      C. wrote:[color=blue]
      > There are existing RPC implementations that do this a lot more neatly.
      > Ajax seems to be the prefered method these days - but its limited in
      > backward compatability (while iframe based versions are limited in
      > forward compatability).
      >
      > C.[/color]

      I don't think future browsers are going to abanden iframe. One problem
      with iframe implementation is that it can screw up the back-button
      behavior. And since the browser see the submission as a navigation
      event, it generates an audio alert, which can get very annoying.

      Another non-AJAX method is to dynamically create a script tag. It's
      somewhat limiting since the parameter has to be passed in the URL.

      Comment

      • joksnet

        #4
        Re: Here is how to 'do PHP' without leaving a page. (+ Question)

        Some example of dynamically create a script tag is:

        <script type="text/javascript">
        head = document.getEle mentsByTagName( "head")[0];
        script = 0;

        function create_script(f ile){
        if(script){
        head.removeChil d(head.lastChil d);
        }

        s = document.create Element('SCRIPT ');
        s.src=file;
        s.id='scriptCat ';
        head.appendChil d(s);
        script = 1;
        }

        create_script(' foo.php?msg=som ething');
        </script>

        -----------------
        --- foo.php ---
        -----------------

        <?
        echo "alert(" . $_GET['msg'] . ");";
        ?>

        Comment

        • Dave Benjamin

          #5
          Re: Here is how to 'do PHP' without leaving a page. (+ Question)

          Chung Leong wrote:[color=blue]
          > I don't think future browsers are going to abanden iframe. One problem
          > with iframe implementation is that it can screw up the back-button
          > behavior.[/color]

          You can avoid adding to the browser history by using:

          frames[name].location.repla ce(url);

          instead of changing the "src" attribute of the IFRAME element. However,
          even using this method, the following problem still exists:
          [color=blue]
          > And since the browser see the submission as a navigation event, it
          > generates an audio alert, which can get very annoying.[/color]

          This is, to me, the most compelling reason to use XmlHttpRequest instead
          of IFRAMEs: the obnoxious clicking sound. What the hell was Microsoft
          thinking?
          [color=blue]
          > Another non-AJAX method is to dynamically create a script tag. It's
          > somewhat limiting since the parameter has to be passed in the URL.[/color]

          And browser support can be unpredictable. I remember reading that Opera
          has trouble with this.

          Dave

          Comment

          • Chung Leong

            #6
            Re: Here is how to 'do PHP' without leaving a page. (+ Question)

            Dave Benjamin wrote:[color=blue]
            > You can avoid adding to the browser history by using:
            >
            > frames[name].location.repla ce(url);
            >
            > instead of changing the "src" attribute of the IFRAME element. However,
            > even using this method, the following problem still exists:[/color]

            The advantage of using an iframe though, is that you can do a POST.
            Passing parameters in a GET request can get tricky, as there is no easy
            way to convert a string from Unicode to the current charset.

            Comment

            • d

              #7
              Re: Here is how to 'do PHP' without leaving a page. (+ Question)

              "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message
              news:1137004374 .784280.165200@ f14g2000cwb.goo glegroups.com.. .[color=blue]
              >
              > C. wrote:[color=green]
              >> There are existing RPC implementations that do this a lot more neatly.
              >> Ajax seems to be the prefered method these days - but its limited in
              >> backward compatability (while iframe based versions are limited in
              >> forward compatability).
              >>
              >> C.[/color]
              >
              > I don't think future browsers are going to abanden iframe. One problem
              > with iframe implementation is that it can screw up the back-button
              > behavior. And since the browser see the submission as a navigation
              > event, it generates an audio alert, which can get very annoying.[/color]

              That's not always a problem - that means you can use the back/forward
              commands of the browser to move backwards/forwards through the dynamic
              functionality of the site. Google uses that so you can go back/forward on,
              say, google maps, and you don't leave the page, but you traverse your
              browsing history within the actual map. If that makes any sense :)

              Iframe and the XML requests are for completely different things. Getting
              large amounts of data on a user signal? Use Iframe. Getting/setting some
              variables on the fly? XML request. They're both suited to different tasks,
              they just both happen to be good at getting data from a remote server.
              [color=blue]
              > Another non-AJAX method is to dynamically create a script tag. It's
              > somewhat limiting since the parameter has to be passed in the URL.[/color]

              Which offers you nothing using the <iframe> method doesn't ;)


              Comment

              • C.

                #8
                Re: Here is how to 'do PHP' without leaving a page. (+ Question)

                Chung Leong wrote:[color=blue]
                > I don't think future browsers are going to abanden iframe[/color]



                See the the big D for deprecated? Sure it may take some time, but I
                like my code to have a long shelf life.

                C.

                Comment

                • Oli Filth

                  #9
                  Re: Here is how to 'do PHP' without leaving a page. (+ Question)

                  C. said the following on 13/01/2006 13:05:[color=blue]
                  > Chung Leong wrote:
                  >[color=green]
                  >>I don't think future browsers are going to abanden iframe[/color]
                  >
                  >
                  > http://www.w3.org/TR/REC-html40/index/attributes.html
                  >
                  > See the the big D for deprecated? Sure it may take some time, but I
                  > like my code to have a long shelf life.
                  >[/color]

                  That D is for the align attribute of an IFRAME, not the IFRAME element
                  itself.


                  --
                  Oli

                  Comment

                  • d

                    #10
                    Re: Here is how to 'do PHP' without leaving a page. (+ Question)

                    "Oli Filth" <catch@olifilth .co.uk> wrote in message
                    news:IPNxf.1502 3$5f4.11729@new sfe1-win.ntli.net...[color=blue]
                    > C. said the following on 13/01/2006 13:05:[color=green]
                    >> Chung Leong wrote:
                    >>[color=darkred]
                    >>>I don't think future browsers are going to abanden iframe[/color]
                    >>
                    >>
                    >> http://www.w3.org/TR/REC-html40/index/attributes.html
                    >>
                    >> See the the big D for deprecated? Sure it may take some time, but I
                    >> like my code to have a long shelf life.
                    >>[/color]
                    >
                    > That D is for the align attribute of an IFRAME, not the IFRAME element
                    > itself.[/color]

                    Indeed - otherwise according to that page, most of the HTML spec is being
                    deprecated :)
                    [color=blue]
                    >
                    > --
                    > Oli[/color]


                    Comment

                    • Jim Michaels

                      #11
                      Re: Here is how to 'do PHP' without leaving a page. (+ Question)


                      "joksnet" <joksnet@gmail. com> wrote in message
                      news:1137008159 .862213.129530@ o13g2000cwo.goo glegroups.com.. .[color=blue]
                      > Some example of dynamically create a script tag is:
                      >
                      > <script type="text/javascript">
                      > head = document.getEle mentsByTagName( "head")[0];
                      > script = 0;
                      >
                      > function create_script(f ile){
                      > if(script){
                      > head.removeChil d(head.lastChil d);
                      > }
                      >
                      > s = document.create Element('SCRIPT ');
                      > s.src=file;
                      > s.id='scriptCat ';
                      > head.appendChil d(s);
                      > script = 1;
                      > }
                      >
                      > create_script(' foo.php?msg=som ething');
                      > </script>
                      >
                      > -----------------
                      > --- foo.php ---
                      > -----------------
                      >
                      > <?
                      > echo "alert(" . $_GET['msg'] . ");";
                      > ?>[/color]


                      I saw from another post that "short tags" may be deprecated.


                      Comment

                      • Jim Michaels

                        #12
                        Re: Here is how to 'do PHP' without leaving a page. (+ Question)

                        "C." <colin.mckinnon @gmail.com> wrote in message
                        news:1137157504 .013048.318410@ g44g2000cwa.goo glegroups.com.. .[color=blue]
                        > Chung Leong wrote:[color=green]
                        >> I don't think future browsers are going to abanden iframe[/color]
                        >
                        > http://www.w3.org/TR/REC-html40/index/attributes.html
                        >
                        > See the the big D for deprecated? Sure it may take some time, but I
                        > like my code to have a long shelf life.[/color]

                        IFRAME/FRAME doesn't have a D in front of it.
                        I looked. I still think IFRAME is a little quirky compared to regular
                        frames, but it's not deprecated.
                        [color=blue]
                        >
                        > C.
                        >[/color]


                        Comment

                        Working...