如何在外部php文件中使用echo函数在JavaScript中定义变量?如何使用PHP echo函数在JavaScript中定义变量?
我们有theconfigfile.php,thejsfile.js和thephpfile.php。
在theconfigfile.php我们:
<?php
$path = 'http://example.com/home.php'; // Set your path
?>
在thejsfile.js我们:
...
if (confirm("Are you sure you want to delete")) {
$.ajax({
type: "POST",
url: "http://example.com/home.php",
data: dataString,
cache: false
});
...
而且在thephpfile.php我们:
<php
include "theconfigfile.php";
?>
<html>
<head>
<script type="text/javascript" src="thejsfile.js"></script>
</head>
<body>
...here is the code that uses file thejsfile.js...
</body>
</html>
我用这种方法:
...
if (confirm("Are you sure you want to delete")) {
$.ajax({
type: "POST",
url: "<?php echo $path; ?>",
data: dataString,
cache: false
});
...
只有当javascript是代码的一部分时才有效。如果我用它外部,像这样...
<script type="text/javascript" src="thejsfile.js"></script>
...不工作!解决方案?
你为什么要通过PHP回显路径?为什么不通过对url进行硬编码来实现,或者将路径分配给变量供以后使用? –
在线php中使用ajax似乎是矛盾的,而不是最好的想法IMO。 – elclanrs
因为我有很多文件,并且我想修改一个文件。 – GAD