简体   繁体   中英

defined path from php to javascript

I have a little problem with the syntax in Javascript. I want to work with a defined variable for a path in 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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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