Two modes of interaction between PHP and JavaScript

Source: Internet
Author: User
Tags implement php and rand
How to make the front page of the page and
Background CGI pages remain interactive for a long time. Here are two I make in practice
The method used.

Method One: Interact with cookies. Altogether there are three files, respectively:
Index.htm,action.php,main.htm
Principle for foreground page main.htm and backstage action.php through page frame
index.htm, the action.php page width is set to 0, which does not
Impact display. Action.php the information into a cookie, main.htm by reading
Cookies to implement the interaction. In Main.htm, you can also read action.php by re-read
To implement the control of the background CGI program.

Index.htm
---------------------------------------------------------------
<title>Test</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<frameset framespacing= "0" border= "false" frameborder= "0" cols= "0,*" >
<frame name= "Leftframe" scrolling= "no" noresize src= "action.php" >
<frame name= "Rightframe" scrolling= "Auto" src= "main.htm" >
</frameset><noframes>
<body bgcolor= "#FFFFFF" >
<p> page frames are used on this page, but your browser is not supported. </p>
</body>
</noframes>
---------------------------------------------------------------

action.php
---------------------------------------------------------------
?
Srand (Double) microtime () *1000000);
$result =rand (0,100);
Setcookie ("Action", $result, Time () +900, "/");
?>
---------------------------------------------------------------

Main.htm
---------------------------------------------------------------
<title>Test</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<script language= "JavaScript" >
function Get_cookie ()
{
Document.test.current_cookie.value=document.cookie;
}
</script>
<body bgcolor= "#FFFFFF" >
<form name= "Test" >
The current parameter is <input type= "text" name= "Current_cookie" size= "M" maxlength= "1000" >
</form>
<script language= "JavaScript" >
SetInterval ("Get_cookie ()", 200);
</script>
<br>
<a href= "action.php" target= "Leftframe" > re-read cookie</a>
</body>
---------------------------------------------------------------

Method Two: Directly through Parent.*.* to realize the interaction. Altogether there are three files, respectively:
Index.htm,action.php,main.htm, where the index.htm is the same as the front.
Principle for direct transmission through Parent.rightFrame.test.current_cookie.value
Information.

action.php
---------------------------------------------------------------
?
Srand (Double) microtime () *1000000);
$result =rand (0,100);
?>
<script language= "JavaScript" >
Parent.rightframe.test.current_cookie.value= ". echo $result?> ";
</script>
---------------------------------------------------------------

Main.htm
---------------------------------------------------------------
<title>Test</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body bgcolor= "#FFFFFF" >
<form name= "Test" >
The current parameter is <input type= "text" name= "Current_cookie" size= "M" maxlength= "1000" >
</form>
<br>
<a href= "action.php" target= "Leftframe" > re-read cookie</a>
</body>
---------------------------------------------------------------


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.