You are here: Here is how to 'do PHP' without leaving a page. (+ Question) « PHP Programming Language « IT news, forums, messages
Here is how to 'do PHP' without leaving a page. (+ Question)

Posted by Peter Fox on 01/11/06 16:37

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=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.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(document.frm.bar.value),
'','resizable=no,toolbar=no,scrollbars=no,status=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(!$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>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация