首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从JavaScript调用php函数

从JavaScript调用php函数
EN

Stack Overflow用户
提问于 2011-08-24 01:54:51
回答 2查看 285.3K关注 0票数 80

有没有办法通过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函数。

EN

回答 2

Stack Overflow用户

发布于 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)函数,该函数将数组作为参数。

票数 11
EN

Stack Overflow用户

发布于 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/"]
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7165395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档