有没有办法通过JS函数运行php函数?
如下所示:
<script type="text/javascript">
function test(){
document.getElementById("php_code").innerHTML="<?php
query("hello"); ?>";
}
</script>
<a href="#" style="display:block; color:#000033; font-family:Tahoma; font-size:12px;"
onclick="test(); return false;"> test </a>
<span id="php_code"> </span>
我基本上想运行php函数query("hello")
,当我点击名为"Test“的href时,它将调用php函数。
发布于 2011-08-24 02:02:13
从JS执行PHP的惟一方法是AJAX。您可以将数据发送到服务器(例如,获取/ajax.php?do=someFunction),然后在ajax.php中编写:
function someFunction() {
echo 'Answer';
}
if ($_GET['do'] === "someFunction") {
someFunction();
}
然后,用JS捕捉答案(我使用jQuery发出AJAX请求)
也许你需要一些回答的格式。请参阅JSON或XML,但是JSON很容易与JavaScript一起使用。在PHP中,您可以使用json_encode($array)函数,该函数将数组作为参数。
发布于 2013-01-08 23:02:39
我最近发布了一个PHP插件,它允许您以各种方式调用jQuery函数:https://github.com/Xaxis/jquery.php
简单的示例用法:
// Both .end() and .data() return data to variables
var strLenA = P.strlen('some string').end();
var strLenB = P.strlen('another string').end();
var totalStrLen = strLenA + strLenB;
console.log( totalStrLen ); // 25
// .data Returns data in an array
var data1 = P.crypt("Some Crypt String").data();
console.log( data1 ); // ["$1$Tk1b01rk$shTKSqDslatUSRV3WdlnI/"]
https://stackoverflow.com/questions/7165395
复制相似问题