<button type="button" id="okButton" onclick="funk()" value="okButton">Order now </button>
<script type="text/javascript">
function funk(){
alert("asdasd");
<?php echo "asdasda";?>
}
</script>
当按下按钮时,我想执行php代码(此时回显asadasda)
答案 0 :(得分:15)
你可以使用 http://phpjs.org/ http://locutus.io/php/它将一堆PHP功能移植到javascript,但是如果它只是回声,并且脚本是在php文件中,你可以做这样的事情:
alert("<?php echo "asdasda";?>");
不要担心使用双引号的漂亮外观,PHP会在浏览器看到之前呈现它。
至于使用ajax,最简单的方法是使用像jQuery这样的库。你可以这样做:
$.ajax({
url: 'test.php',
success: function(data) {
$('.result').html(data);
}
});
和test.php将是:
<?php
echo 'asdasda';
?>
它会将test.php的内容写入任何具有result
类的元素。
答案 1 :(得分: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>
假设我们在某处有一个ID =“frameContent”的html文件, 然后我们可以用简单的&lt;改变显示。 body onload =“updateContent()”&gt;
Golly gee;我们现在不需要PHP来做到这一点!但这创造了一个结构 应用PHP提供的内容。
我们将有问题的网页更改为PHTML类型,以允许服务器端PHP访问 内容:
**foo.html becomes foo.phtml**
我们将添加到该页面的顶部。我们还会导致加载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');
?>
现在我们的javascripts可以像这样访问PHP全局:
//通过accessig全局变量 var textMsg ='&lt; ? php global $ textMsgPHP; echo“$ textMsgPHP”; ? &GT;';
在javascript中,替换
var textMsg ='祝晚安Gracy';
使用: //使用php returnContent()
var textMsg ='&lt; ? php $ msgX = returnContent('dummy_div_data_3.txt'); echo“$ msgX”? &GT;';
要点:
要解决:使用文件名和调用 updateContent() 通过 onClick()而不是 onLoad()
使用它可以在Sample_Dynamic_Frame.zip中提供一个示例供您检查,但没有找到附加它的方法
答案 2 :(得分:4)
您无法使用javascript运行PHP。 JavaScript是一种客户端技术(在用户浏览器中运行),PHP是服务器端技术(在服务器上运行)。
如果你想这样做,你必须向PHP脚本发出ajax请求,并返回你要查找的结果。
你为什么要这样做?
答案 3 :(得分:2)
如果您只想在用户点击按钮时在页面上的某个位置回显来自PHP的消息,您可以执行以下操作:
<button type="button" id="okButton" onclick="funk()" value="okButton">Order now</button>
<div id="resultMsg"></div>
<script type="text/javascript">
function funk(){
alert("asdasd");
document.getElementById('resultMsg').innerHTML('<?php echo "asdasda";?>');
}
</script>
但是,假设您的脚本需要进行一些服务器端处理,例如将项目添加到购物车,您可能想查看jQuery的http://api.jquery.com/load/ - 使用jQuery加载php脚本的路径处理。在您的示例中,您可以这样做:
<button type="button" id="okButton" onclick="funk()" value="okButton">Order now</button>
<div id="resultMsg"></div>
<script type="text/javascript">
function funk(){
alert("asdasd");
$('#resultMsg').load('path/to/php/script/order_item.php');
}
</script>
这将运行php脚本并将其返回的任何消息加载到<div id="resultMsg">
。
order_item.php会将该项目添加到购物车,并回显您要显示的任何消息。为了使示例正常工作,这就足够了order_item.php:
<?php
// do adding to cart stuff here
echo 'Added to cart';
?>
要实现此功能,您需要在页面中添加jQuery,方法是在<head>
标记中添加:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
答案 4 :(得分:1)
任何服务器端内容(例如php声明)都必须在脚本文件(例如以下)中的主机文件(扩展名为.php的文件)中进行评估
<script type="text/javascript">
var1 = "<?php echo 'Hello';?>";
</script>
然后在.js文件中,您可以使用变量
alert(var1);
如果您尝试评估.js文件中的php声明,它将无法正常工作
答案 5 :(得分:1)
我们可以在 JavaScript 中使用 php,方法是创建一个表单元素并将操作作为 .php 页面。 然后我们使用 JavaScript 提交该表单。 例如:
<!doctype html>
<html>
<head>
<title>PHP Executed with JS</title>
</head>
<body>
<form action="phpCode.php" id="phpCode">.
</form> <!-- This is the form-->
<script>
function runPhp() {
var php =
document.getElementById("phpCode")
php.submit() //submit the form
}
</script>
</body>
PHP 文件名将是 phpCode.php。 在该文件中将是您的 PHP 代码。
答案 6 :(得分:0)
可能就是这样:
<?php
if($_SERVER['REQUEST_METHOD']=="POST") {
echo 'asdasda';
}
?>
<form method="post">
<button type="submit" id="okButton">Order now</button>
</form>
答案 7 :(得分:0)
如果您不想包含jquery库,可以简单地执行以下操作
a)广告iframe,尺寸为0px,因此不可见,href为空白
b)在你的js代码函数
中执行它 window.frames['iframename'].location.replace('http://....your.php');
这将执行php脚本,您可以进行数据库更新...
答案 8 :(得分:0)
将你的php放入隐藏的div中,然后用javascript调用它
php部分
<div id="mybox" style="visibility:hidden;"> some php here </div>
javascript部分
var myfield = document.getElementById("mybox");
myfield.visibility = 'visible';
现在,你可以用myfield做任何事......
答案 9 :(得分:0)
使用ajax发送请求并回显响应 成功执行时。像这样:
const makeAccount = async () => {
console.log(credentials);
await axios.post("http://localhost:3001/signup", credentials);
};
这可以通过 jquery 库来实现。
答案 10 :(得分:-20)
function echoHello()
{alert ("<?php funkx(); ?>");}
此代码应该为您提供一个弹出窗口