<button type="button" id="okButton" onclick="funk()" value="okButton">Order now </button>
<script type="text/javascript">
function funk(){
alert("asdasd");
<?php echo "asdasda";?>
}
</script>
Run Code Online (Sandbox Code Playgroud)
当按下按钮时我想执行php代码(此时回显asadasda)
JKi*_*rtz 16
你可以使用http://phpjs.org/ http://locutus.io/php/它将一堆PHP功能移植到javascript,但是如果它只是回声,并且脚本是在php文件中,你可以做一些事情像这样:
alert("<?php echo "asdasda";?>");
Run Code Online (Sandbox Code Playgroud)
不要担心使用双引号的漂亮外观,PHP会在浏览器看到它之前呈现它.
至于使用ajax,最简单的方法是使用像jQuery这样的库.有了这个你可以做:
$.ajax({
url: 'test.php',
success: function(data) {
$('.result').html(data);
}
});
Run Code Online (Sandbox Code Playgroud)
和test.php将是:
<?php
echo 'asdasda';
?>
Run Code Online (Sandbox Code Playgroud)
它会将test.php的内容写入具有result该类的任何元素.
小智 6
Javascript和PHP的交互
我们都知道Javascript在客户端(即浏览器)上运行,而PHP是服务器端工具(即服务器端).很明显这两个人无法互动.
但是 - 好消息; 它可以工作,这是如何.
目标是从服务器获取一些动态信息(比如服务器配置项)到Javascript环境中,以便在需要时使用它 - 通常这意味着DHTML对表示的修改.
首先,为了澄清DHTML的用法,我将引用这个DHTML示例:
<script type="text/javascript">
function updateContent() {
var frameObj = document.getElementById("frameContent");
var y = (frameObj.contentWindow || frameObj.contentDocument);
if (y.document) y = y.document;
y.body.style.backgroundColor="red"; // demonstration of failure to alter the display
// create a default, simplistic alteration usinga fixed string.
var textMsg = 'Say good night Gracy';
y.write(textMsg);
y.body.style.backgroundColor="#00ee00"; // visual confirmation that the updateContent() was effective
}
</script>
Run Code Online (Sandbox Code Playgroud)
假设我们在某处有一个ID ="frameContent"的html文件,那么我们可以用一个简单的<body onload ="updateContent()">来改变显示
Golly gee; 我们现在不需要PHP来做到这一点!但是这创建了一个应用PHP提供的内容的结构.
我们将有问题的网页更改为PHTML类型,以允许服务器端PHP访问内容:
**foo.html becomes foo.phtml**
Run Code Online (Sandbox Code Playgroud)
我们将添加到该页面的顶部.我们还将php数据加载到全局变量中以供以后访问 - 如下所示:
<?php
global $msg1, $msg2, $textMsgPHP;
function getContent($filename) {
if ($theData = file_get_contents($filename, FALSE)) {
return "$theData";
} else {
echo "FAILED!";
}
}
function returnContent($filename) {
if ( $theData = getContent($filename) ) {
// this works ONLY if $theData is one linear line (ie remove all \n)
$textPHP = trim(preg_replace('/\r\n|\r|\n/', '', $theData));
return "$textPHP";
} else {
echo '<span class="ERR">Error opening source file :(\n</span>'; # $filename!\n";
}
}
// preload the dynamic contents now for use later in the javascript (somewhere)
$msg1 = returnContent('dummy_frame_data.txt');
$msg2 = returnContent('dummy_frame_data_0.txt');
$textMsgPHP = returnContent('dummy_frame_data_1.txt');
?>
Run Code Online (Sandbox Code Playgroud)
现在我们的javascripts可以像这样访问PHP全局变量:
//通过accessig the globals var textMsg ='<?php global $ textMsgPHP; echo"$ textMsgPHP"; ?>';
在javascript中,替换
var textMsg ='祝晚安Gracy';
with://使用php returnContent()
var textMsg ='<?php $ msgX = returnContent('dummy_div_data_3.txt'); echo"$ msgX"?>';
摘要:
要解决:使用文件名调用updateContent()并通过onClick()而不是onLoad()使用它
可以在Sample_Dynamic_Frame.zip中提供一个示例供您检查,但没有找到附加它的方法
你不能用 JavaScript 来运行 PHP。JavaScript 是一种客户端技术(在用户浏览器中运行),而 PHP 是一种服务器端技术(在服务器上运行)。
如果你想这样做,你必须向 PHP 脚本发出 ajax 请求,并让它返回你正在寻找的结果。
你为什么要这样做?