简体   繁体   English

从php到javascript的定义路径

[英]defined path from php to javascript

I have a little problem with the syntax in Javascript. 我对Javascript的语法有一点疑问。 I want to work with a defined variable for a path in Javascript. 我想为Javascript中的路径使用定义的变量。

    function checkusername(){
        var u = _("username").value;
        if(u != ""){
              _("unamestatus").innerHTML = 'checking ...';
              var ajax = ajaxObj("POST", "http://localhost:8888/.../file.php");
              ajax.onreadystatechange = function() {
                    if(ajaxReturn(ajax) == true) {
                          _("unamestatus").innerHTML = ajax.responseText;
                    }
              }
              ajax.send("usernamecheck="+u);
        }
    }

Now I want to set for 现在我要设定

http://localhost:8888/.../file.php

a defined variable from php 从PHP定义的变量

define('Name','http://localhost:8888/.../file.php');

You'd either have to retrieve that constant via an AJAX call, or embed it into the Javascript at the time PHP is building the page. 您要么必须通过AJAX调用来检索该常量,要么在PHP构建页面时将其嵌入到Javascript中。

eg 例如

<?php
define('your_url', 'http://.....');
?>

<script type="text/javascript">

var url = <?php echo json_encode(your_url) ?>;

...
     var ajax = ajaxOBJ('POST', url);

Note that if the sole purpose of this constant is to hold a url that's passed to javascript and is otherwise never used in PHP, you might as well just use a variable - Javascript could not alter the PHP/server-side value anyways, so it's effectively a constant. 请注意,如果此常量的唯一目的是保存传递给javascript的URL,否则将不会在PHP中使用该URL,那么您也可以只使用一个变量-Javascript仍然无法更改PHP /服务器端的值,因此有效地是一个常数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM