javascript 與 php 之間的互相呼叫
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程式碼:
<?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>
(3)
<script language="JavaScript">
<!--
var Y=<?php echo date('Y')?>,M=<?php echo date('n')?>,D=<?php echo date('j')?>;
-->
</script>
5.自己寫的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-->
<script language=''javascrīpt'' type=''text/javascrīpt'' src=''1.php''> </script>
<script>
ok("aaaaaa!");
</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() '>
舉一個簡單的例子來說明:
如在頁面a.html中用下面這句呼叫:
<script type="text/javascript" src="b.php?action=test"></script>
<script type="text/javascript" >
alert(jstext);
</script>
在b.php中有這樣一段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>
(3)
<script language="JavaScript">
<!--
var Y=<?php echo date('Y')?>,M=<?php echo date('n')?>,D=<?php echo date('j')?>;
-->
</script>
5.自己寫的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-->
<script language=''javascrīpt'' type=''text/javascrīpt'' src=''1.php''> </script>
<script>
ok("aaaaaa!");
</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() '>
請問在第一種方式中,只能賦予一個值而以嗎?
回覆刪除我想使用兩個值,但是卻會造成錯誤
羅志聰食燒賣
回覆刪除