js調用php和php調用js的方法

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() '>

發佈留言