SoFunction
Updated on 2025-04-08

Two ways of interaction between PHP and javascript

How to make the front-end page without refreshing the page during web page production process
Keeping interaction with the background CGI page has always been a problem. Here are two things I have made in practice
Method used.

Method 1: Interact through cookies. There are three files in total, namely:
,,
The principle is to use the foreground page and backend through the page frame
Organize and set the page width to 0, so it doesn't
Effect display. Put the information into the cookie and read it
Cookies to enable interaction. It can also be read by rereading it in
To implement the control background CGI program.


---------------------------------------------------------------
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<frameset framespacing="0" border="false" frameborder="0" cols="0,*">
  <frame name="leftFrame" scrolling="no" noresize src="">
  <frame name="rightFrame" scrolling="auto" src="">
</frameset><noframes>
  <body bgcolor="#FFFFFF">
<p>This page uses a page framework, but your browser does not support it. </p>
  </body>
</noframes>
</html>
---------------------------------------------------------------


---------------------------------------------------------------
<?
srand((double)microtime()*1000000);
$result=rand(0,100);
setcookie("action",$result,time()+900,"/");
?>
---------------------------------------------------------------


---------------------------------------------------------------
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
function get_cookie()
{
  .current_cookie.value=;
}
</script>
</head>
<body bgcolor="#FFFFFF">
<form name="test" >
The current parameter is <input type="text" name="current_cookie" size="80" maxlength="1000">
</form>
<script language="javascript">
  setInterval("get_cookie()",200);
</script>
<br>
<a href="" target="leftFrame">Reread the cookie</a>
</body>
</html>
---------------------------------------------------------------

Method 2: directly realize interaction through parent.*.*. There are three files in total, namely:
,,,,, and the same as the previous one.
The principle is to pass directly through .current_cookie.value
information.


---------------------------------------------------------------
<?
srand((double)microtime()*1000000);
$result=rand(0,100);
?>
<script language="javascript">
  .current_cookie.value="<? echo $result?>";
</script>
---------------------------------------------------------------


---------------------------------------------------------------
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<form name="test" >
The current parameter is <input type="text" name="current_cookie" size="80" maxlength="1000">
</form>
<br>
<a href="" target="leftFrame">Reread the cookie</a>
</body>
</html>
---------------------------------------------------------------