Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Peter Fox
Guest
 
Posts: n/a
#1: Jan 11 '06
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="JavaScript">
<!--

function Fly(){
jt = 'wilbur.php?VAL='+escape(document.frm.bar.value) // escape makes
string safe to add to URL
window.open(jt,
'','resizable=no,toolbar=no,scrollbars=no,status=n o,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.toLocaleString() + "</b><p>")
// -->
</SCRIPT>

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

<FORM NAME="frm">
<table bgcolor="#b0b0b0">
<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="javascript:Fly()";> (Automatically validated in
PHP-land)</td></tr>
<!-- alternative in-line version which means the <script...Function
Fly().../script> can be omitted
Bar : <INPUT NAME="bar" SIZE=10 VALUE="pqr"
onBlur="window.open('proofoc6.php?VAL='+escape(doc ument.frm.bar.value),
'','resizable=no,toolbar=no,scrollbars=no,status=n o,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(!$currentValue){
$valbar='(none)';
}else{
$valbar = strrev($currentValue);
if($valbar==$currentValue){$valbar='Palindrome';}
}

// --------- back to the world of Javascript ------
?>
<html><head>
<SCRIPT LANGUAGE="JavaScript">
<!--
opener.document.frm.bar.value='<?php print($valbar);?>'
x=opener.document.frm.foo.value;
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@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>

C.
Guest
 
Posts: n/a
#2: Jan 11 '06

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.

Chung Leong
Guest
 
Posts: n/a
#3: Jan 11 '06

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.

joksnet
Guest
 
Posts: n/a
#4: Jan 11 '06

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.getElementsByTagName("head")[0];
script = 0;

function create_script(file){
if(script){
head.removeChild(head.lastChild);
}

s = document.createElement('SCRIPT');
s.src=file;
s.id='scriptCat';
head.appendChild(s);
script = 1;
}

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

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

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

Dave Benjamin
Guest
 
Posts: n/a
#5: Jan 12 '06

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.replace(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
Chung Leong
Guest
 
Posts: n/a
#6: Jan 12 '06

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.replace(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.

d
Guest
 
Posts: n/a
#7: Jan 12 '06

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.googlegr oups.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 ;)


C.
Guest
 
Posts: n/a
#8: Jan 13 '06

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]

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.

C.

Oli Filth
Guest
 
Posts: n/a
#9: Jan 13 '06

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
d
Guest
 
Posts: n/a
#10: Jan 16 '06

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


"Oli Filth" <catch@olifilth.co.uk> wrote in message
news:IPNxf.15023$5f4.11729@newsfe1-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]


Jim Michaels
Guest
 
Posts: n/a
#11: Jan 22 '06

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



"joksnet" <joksnet@gmail.com> wrote in message
news:1137008159.862213.129530@o13g2000cwo.googlegr oups.com...[color=blue]
> Some example of dynamically create a script tag is:
>
> <script type="text/javascript">
> head = document.getElementsByTagName("head")[0];
> script = 0;
>
> function create_script(file){
> if(script){
> head.removeChild(head.lastChild);
> }
>
> s = document.createElement('SCRIPT');
> s.src=file;
> s.id='scriptCat';
> head.appendChild(s);
> script = 1;
> }
>
> create_script('foo.php?msg=something');
> </script>
>
> -----------------
> --- foo.php ---
> -----------------
>
> <?
> echo "alert(" . $_GET['msg'] . ");";
> ?>[/color]


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


Jim Michaels
Guest
 
Posts: n/a
#12: Jan 22 '06

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.googlegr oups.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]


Closed Thread