如何在JavaScript中使用PHP?
我尝试在JavaScript代码中嵌入PHP,看看会发生什么。实际上什么也没发生。有人能给我一个建议吗如何在JavaScript中使用PHP?,javascript,php,html,Javascript,Php,Html,我尝试在JavaScript代码中嵌入PHP,看看会发生什么。实际上什么也没发生。有人能给我一个建议吗 <head> <script> function myF() { n=document.getElementById("name").value r=document.getElementById("reason").value <?php $f = fopen("VisitorLog.txt", "w");
<head>
<script>
function myF() {
n=document.getElementById("name").value
r=document.getElementById("reason").value
<?php
$f = fopen("VisitorLog.txt", "w");
fwrite($f, "Profile viewed on "+Date()+" by "+n+" Reason= "+r);
fclose($f);
?>
document.getElementById("name").disabled=true;
document.getElementById("reason").disabled=true;
document.getElementById("register").disabled=true;
document.getElementById("link").style.display="inline";
alert("You have successfully registered.");
}
</script>
</head>
函数myF(){
n=document.getElementById(“名称”).value
r=document.getElementById(“原因”).value
document.getElementById(“名称”).disabled=true;
document.getElementById(“原因”).disabled=true;
document.getElementById(“寄存器”).disabled=true;
document.getElementById(“link”).style.display=“inline”;
警报(“您已成功注册”);
}
我对这些东西都不熟悉,还在学习。请试着用更简单的术语解释P
首先要记住,PHP是在服务器上呈现的,Javascript将在客户端(Web浏览器)进行解释。所以,若您从PHP回显某些内容,它将随html一起发送,而不会像您在这里假设的那个样使用Javascript执行
为了实现您在这里想要的功能,您需要对PHP脚本进行AJAX调用,该脚本将更新您的视图日志 编辑: 关于update.php文件<?php
$n = $_POST['n'];
$r = $_POST['r'];
$f = fopen("VisitorLog.txt", "w");
fwrite($f,"Profile viewed on ".date("Y-m-d H:i:s")." by ".$n." Reason= "+$r);
fclose($f);
PhP在加载页面之前在服务器上执行。因此将执行PhP,然后加载页面。然后加载javascript。因此,这里的javascript不会触发PhP
有两种方法可以做到这一点:
-当页面在服务器上运行时,运行PhP脚本
-使用AJAX运行服务器端php脚本以注册用户详细信息。php在上呈现。JavaScript在上呈现。JavaScript在客户端运行,php在服务器端运行。例如,您需要在服务器端调用一些方法来执行服务器端任务。就像大家说的那样。不能将服务器端代码与客户端代码混合使用。。php在服务器端渲染/执行,在客户端浏览器中使用js
如果可能,将js和php分开。一个可行的解决方案可以是:(假设jQuery用于ajax调用,实际上可以使用另一个框架甚至普通js)
在js文件中(或在标题中的脚本标记中)
在文件logger.php
<?php
if(isset($_GET['message'])){
$f = fopen("VisitorLog.txt", "w");
fwrite($f, $_GET['message']);
fclose($f);
}
?>
您需要了解的主要概念是Javascript在客户端运行,即web浏览器,PHP在服务器上运行。添加到页面中的PHP代码在处理任何Javascript之前执行
如果希望Javascript将数据发送到PHP应用程序,则需要使用Ajax。
使用像这样的图书馆会让你的生活更轻松。
下面是一个使用jQuery如何工作的示例
PHP-log.PHP
<?php
$string = sprintf('Profile viewed on %s by %s Reason= %s',
$_POST['date'], $_POST['name'], $_POST['reason']);
$f = fopen("VisitorLog.txt", "w");
fwrite($f, $string);
fclose($f);
?>
参考文献
只有服务器可以解释和运行php代码。确保您正在编辑的文件存储在安装了php的服务器中(如果您想在计算机中进行本地测试,可以尝试WAMP或XAMPP)。您可以在Javascript中使用php,就像这样使用:var jsvar='';现在检查一下,您可以警告这个js变量,如果您的文件具有php扩展ID,那么这将起作用。您将文件保存为.php?您必须使用Ajax
php简单地打印输出。你想做什么?天哪,这是个问题:他如何在php代码中使用js变量?你为什么这样写php
:|那么我如何在没有php的情况下将文本框值保存到文本文件中?看看编辑(这不是完美的,但它起到了作用)你是否在网页中添加了jquery?
<?php
if(isset($_GET['message'])){
$f = fopen("VisitorLog.txt", "w");
fwrite($f, $_GET['message']);
fclose($f);
}
?>
<?php
$string = sprintf('Profile viewed on %s by %s Reason= %s',
$_POST['date'], $_POST['name'], $_POST['reason']);
$f = fopen("VisitorLog.txt", "w");
fwrite($f, $string);
fclose($f);
?>
var n = $("#name").val();
var r = $("#reason").val();
var d = new Date();
$.ajax({
url: '/path/to/log.php',
method: 'POST',
data: {name: n, reason: r, date: d}
}).done(function(response){
// response contains the output of log.php
});