外部のphpファイルからecho関数を使用してJavaScriptで変数を定義するにはどうすればよいですか?
configfile.php、 thejsfile.js、thephpfile.phpがあります。
configfile.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
});
...
そして、phpfile.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>
...動作しません!ソリューション?