473,499 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="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
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Jan 11 '06 #1
11 2415
C.
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.

Jan 11 '06 #2

C. wrote:
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.


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.

Jan 11 '06 #3
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'] . ");";
?>

Jan 11 '06 #4
Chung Leong wrote:
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.
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:
And since the browser see the submission as a navigation event, it
generates an audio alert, which can get very annoying.
This is, to me, the most compelling reason to use XmlHttpRequest instead
of IFRAMEs: the obnoxious clicking sound. What the hell was Microsoft
thinking?
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.


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

Dave
Jan 12 '06 #5
Dave Benjamin wrote:
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:


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.

Jan 12 '06 #6
d
"Chung Leong" <ch***********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...

C. wrote:
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.
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.


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


Which offers you nothing using the <iframe> method doesn't ;)
Jan 12 '06 #7
C.
Chung Leong wrote:
I don't think future browsers are going to abanden iframe


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.

Jan 13 '06 #8
C. said the following on 13/01/2006 13:05:
Chung Leong wrote:
I don't think future browsers are going to abanden iframe

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.


That D is for the align attribute of an IFRAME, not the IFRAME element
itself.
--
Oli
Jan 13 '06 #9
d
"Oli Filth" <ca***@olifilth.co.uk> wrote in message
news:IP*******************@newsfe1-win.ntli.net...
C. said the following on 13/01/2006 13:05:
Chung Leong wrote:
I don't think future browsers are going to abanden iframe

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.


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


Indeed - otherwise according to that page, most of the HTML spec is being
deprecated :)

--
Oli

Jan 16 '06 #10

"joksnet" <jo*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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'] . ");";
?>

I saw from another post that "short tags" may be deprecated.
Jan 22 '06 #11
"C." <co************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Chung Leong wrote:
I don't think future browsers are going to abanden iframe
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.


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.

C.

Jan 22 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2589
by: jbj | last post by:
Something like a php function that can be called? I have php in a page that needs to be update periodically (basically poll results) without reloading the page around it (I do have a button you...
3
3837
by: Markus Keuthmann | last post by:
Hi, is it possible to connect to a DB2 database through PHP WITHOUT ODBC like in MySQL? Example MySQL: // Open the database connection and use the sample // database $connection =...
2
2739
by: ALFRED | last post by:
Hi Using access 2003 I have a front end and back end. I would like to close the connection to the backend so that I can compact the back end without leaving the front end. Currently I must exit...
1
1706
by: Rich | last post by:
Hello, I want to create a cookie that will expire in a few mintues after a user leaves my page so that they can return without having to login again if they were only gone for a few minutes. I...
7
1555
by: lameazoid | last post by:
Ok, so I'm runnting ot run some Wiki software on my machine for personal organizational use. It requires PHP but things look ugly if I try to run it right off the desktop. Is there a way to get...
7
1599
by: PJ6 | last post by:
After resigning myself to really learn JavaScript, I finally made my own grid control that populates itself and updates data . It's actually pretty sweet. The only problem I have with it is that...
2
2472
by: Ben | last post by:
Hi, One ASP.NET transactional page conducts a long transaction in a button click function. I want to display the transaction progress info in label control without refreshing page. It is...
0
1208
by: Nichu | last post by:
Hello Lately I started to "bravely fight" with Postfix + Squirrelmail everything works great (PHP also :)) but I want to have possibility to change passwords (from MySql database) through the...
2
1476
by: kruczek | last post by:
Hi, I try add logo for myspace and appear "bug" or logo is here but without background. I need help, I am vocalist of band we always do by onself... but now I don't know what should I do.... ...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7395
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5485
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3108
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.