如何用PHP的echo函数在JavaScript中定义一个变量? [英] How to define a variable in JavaScript with PHP echo function?

查看:155
本文介绍了如何用PHP的echo函数在JavaScript中定义一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JavaScript中使用echo函数从外部php文件中定义变量?



我们有 theconfigfile.php thejsfile.js thephpfile.php



theconfigfile.php

 <?php 
$ path ='http://example.com/home.php'; //设置你的路径
?>

thejsfile.js 中:

  ... 
if(confirm(您确定要删除)){
$ .ajax({
类型:POST,
url:http://example.com/home.php,
data:dataString,
cache:false
}) ;
...

thephpfile.php 中,我们有:

 < php 
包括theconfigfile.php;
?>

< html>
< head>
< script type =text / javascriptsrc =thejsfile.js>< / script>
< / head>

< body>
...这是使用file thejsfile.js的代码...
< / body>
< / html>

我使用这种方法:

<$ p $如果(确认(你确定要删除)){
$ .ajax({
type:POST,
url:<?php echo $ path;?>,
data:dataString,
cache:false
});
...

只有当JavaScript是代码的一部分时才可用。如果我在外部使用它,像这样...

 < script type =text / javascriptsrc =thejsfile的.js>< /脚本> 

...不起作用!解决方案?

解决方案

您可以通过几种方法来做到这一点。



您可以将您的网络服务器配置为使用PHP处理扩展名为 .js 的文件,并在其中注入您的PHP。当然,这意味着你需要一种方法来真正计算你的变量,这会减缓你的常规javascript内容。



你可以简单地将PHP变量输出到在< script> 元素中的Javascript变量像这样

 < script type =text / javascript> 
var path =<?php echo $ path;?>;
< / script>

然后在您的路径变量中访问AJAX。大多数人可能会使用第二种方法。


How do I define a variable in javascript with echo function, from the external php file?

We have theconfigfile.php, thejsfile.js and thephpfile.php.

In theconfigfile.php we have:

<?php
$path = 'http://example.com/home.php'; // Set your path
?>

In thejsfile.js we have:

... 
if (confirm("Are you sure you want to delete")) {
        $.ajax({
            type: "POST",
            url: "http://example.com/home.php",
            data: dataString,
            cache: false
        });
...

And in thephpfile.php we have:

    <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>

I used this method:

... 
if (confirm("Are you sure you want to delete")) {
        $.ajax({
            type: "POST",
            url: "<?php echo $path; ?>",
            data: dataString,
            cache: false
        });
...

Only works when javascript is part of the code. And if I use it external, like this...

<script type="text/javascript" src="thejsfile.js"></script>

...does not work! Solutions?

解决方案

There are a couple of ways you can go about doing this.

You can configure your webserver to process files with extension .js with PHP and just inject your PHP there. Of course this means you need a way to actually calculate your variable there, and this would slow down serving your regular javascript content.

You can simply output the PHP variable to a Javascript variable within a <script> element like this

<script type="text/javascript">
var path = "<?php echo $path; ?>";
</script>

And then access this path variable in your AJAX. Most would probably use the second approach.

这篇关于如何用PHP的echo函数在JavaScript中定义一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆