1 JS方式調用PHP檔案並取得php中的值
舉一個簡單的例子來說明:
如在頁面a.html中用下面這句調用:
<script type="text/javascript" src="b.php?action=test"></script>
<script type="text/javascript" >
alert(jstext);
</script>
在b.php中有這樣一段PHP代碼:
<? $action=$_GET['action']; //echo "var jstext='$action'"; //輸出一句JS語句,產生一個JS變數,並賦顛值為PHP變數 $action的值 //echo "var jstext='aa'"; echo "var jstext="."'$action'"; ?>
當執行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> <? $key="<script type=text/javascript>document.write(url)</script>"; echo $key; ?>
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)
<?php
$userId=100;
?> <script>
var userId;
userId=document.getElementByIdx_x_x_x("userId").value;
alert (userId);
</script>
<input type="text" name="userId" id="userId" value="<?php echo $userId; ?>">
(2)
<?php
$url = '變化的網址'; //定義變數
?>
<script type="text/javascript">
//js調用php變數
var ds ="<?php echo $url?>" ; //賦值 alert(ds); //輸出效果 </script>
5 -------------------------------
<script language="JavaScript"> <!--
var Y=<?php echo date('Y')?>,M=<?php echo date('n')?>,D=<?php echo date('j')?>;
-->
</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() '>