js php 互调
1 JS方式调用PHP文件并取得php中的值
举一个简单的例子来说明:
如在页面a.html中用下面这句调用:
<! DOCTYPE HTML>
<html>
<head>
</head>
<body>
<script src="test.php?action=tst">
</script>
<script> alert(jstext);</script>
</body>
</html>
在b.php中有这样一段PHP代码:
<?php
$str = $_GET['action'];
echo "jstext="."'$str'";//echo "jstext='$str'";
?>
当执行a.html文件时,就会调用b.php文件,并将b.php文件的输出作为JS语句来执行,所以此处会弹出一个提示框,内容为JS变量jstext的值,也就是在PHP文件中赋给jstext的值.
小结:
在HTML里用JS 调用文件的方式调PHP文件,则PHP文件的输出将会被调用页作为JS的代码来用.
2 php调用js中的值
在z.php页面中有这样一段代码:
<script type="text/javascript" > var url="aaaa*"; </script>
<?php
$key="<script type=text/javascript>document.write(url)</script>";
echo "<b>".$key." </b>";
?>
3 php调用js中的方法(函数)
<script type="text/javascript"> function test() { var t1=3; t1 = t1+2; alert(t1); //return t1; } </script>
<?php echo "<script type='text/javascript'>test();</script>"; ?>
4 JS调用PHP变量值
(1)test.php
<html>
<head>
</head>
<body>
<?php
$str = "test";
?>
<input type="text" id="txt1" value="<?php echo $str; ?>" />
<script>
var str;
str = document.getElementById("txt1").value;
alert(str);
</script>
</body>
</html>
(2)test2.php
<?php
$url = '变化的网址'; //定义变量
?>
<script type="text/javascript">
//js调用php变量
var ds ="<?php echo $url?>" ; //
赋值 alert(ds); //输出效果 </script>
6 自己写的js和php互相调用
1.php内容:
<?php
//echo "<script LANGUAGE='javascript'>alert('$php变量');</script>"; //最简单的php调用js
//echo "<a href=#><img width=50 src='$fruit_pic_array[$i]' onMouseOver=’javascript:a();‘></a>";
//echo "<a href='3.php'>aaaa</a>"; //php中超链接
//echo "<script type='text/javascript' language='javascript'>phpmake('PHP建站学习笔记网');</script>"; //有时候需要在PHP执行过程中,需要调用JavaScript自定义函数(验证时出错)
echo "function ok(msg){alert(msg);}";
?>
<HTML>
<HEAD>
<TITLE> php调用js文件的好办法</TITLE>
</HEAD>
<BODY>
<!--js调用php中定义的js-->
<scrīpt language=''javascrīpt'' type=''text/javascrīpt'' src=''1.php''></scrīpt>
<scrīpt>
ok("aaaaaa!");
</scrīpt>
</script>
</BODY>
</HTML>
2.php内容:
<!--js调用php-->
<?php
$userId=100;
?>
<script>
var userId;
userId=document.getElementByIdx_x("userId").value;
alert (userId);
</script>
<input type="text" name="userId" id="userId" value="<?php echo $userId; ?>">
<!--js调用php-->
<?php
if($_GET["action"]=="ok")
{
echo "I'm OK!";
}
else
{
echo "I'm not OK!";
}
?>
<SCRIPT Language = "JavaScript">
function func()
{
if(confirm("Are you OK with this?"))
{
this.location = "ok.php?action=ok";
}
else
{
this.location = "ok.php?action=cancel";
}
}
</SCRIPT>
<html>
<head>
</head>
<body>
<a href="#" href="#" onClick="javascript:func();">Please Click</a>
</body>
</html>
<!--js调用php-->
<html>
<head>
<script>
function isMail(PostString)
{
re=/\w*/
if(re.test(PostString))
{
return true;
}
else
{
return false;
}
}
function test(){
if (isMail(<?php echo $email?>))
{document.write("<?php echo "N";?>");}
else
{document.write('<?php echo 'Y';?>');}
}
</script>
</head>
<body>
<?php
$email="aa";
?>
<input type=button value=click onclick= 'test() '>
</body>
</html>
<!--php中含有js代码-->
<?php
echo "
<script language=javascript>
function test(){
alert( 'hello ');
}
</script> ";
?>
<input type=button value=click onclick= 'test() '>