简体   繁体   English

PHP值在外部jQuery Scipt内不起作用

[英]Php value won't work inside external jquery scipt

I am having a hard time finding a good solution to what I am facing right now. 我很难为我现在所面临的问题找到一个好的解决方案。 I am currently building an upload function to handle upload files. 我目前正在构建上传功能来处理上传文件。 I used a jquery file upload plugin from Hayageek . 我使用了来自Hayageek的jquery文件上传插件。 Now the upload works fine but when I moved the script to a separate js file and load it as an external script, the url won't work. 现在上传可以正常工作,但是当我将脚本移到一个单独的js文件并将其作为外部脚本加载时,该网址将无法工作。

This is the code I used. 这是我使用的代码。

$(document).ready(function()
{
    $("#fileuploader").uploadFile({
    url:"<?php echo site_url('document_items/upload') ?>",
    fileName:"myfile"
    });
});

Use document.location.origin or window.location.protocol . 使用document.location.originwindow.location.protocol

var baseurl = window.location.protocol + "//" + window.location.host + "/";
var newurl = baseurl + 'document_items/upload';

    $(document).ready(function()
{
    $("#fileuploader").uploadFile({
    url: newurl,
    fileName:"myfile"
    });
});

For Javascript 对于Javascript

window.location.host          #returns host
window.location.hostname      #returns hostname
window.location.path          #return path
window.location.href          #returns full current url
window.location.port          #returns the port
window.location.protocol      #returns the protocol

For Jquery 对于jQuery

$(location).attr('host');        #returns host
$(location).attr('hostname');    #returns hostname
$(location).attr('path');        #returns path
$(location).attr('href');        #returns href
$(location).attr('port');        #returns port
$(location).attr('protocol');    #returns protocol

javascript is client side script and php is server side, so you may not use php in javascript file. javascript是客户端脚本,而php是服务器端,因此您不能在javascript文件中使用php。

I would suggest you to use javascript tag in php/phtml as below 我建议您使用php / phtml中的 javascript标签,如下所示

<script type="text/javascript">
    $(document).ready(function()
{
    $("#fileuploader").uploadFile({
    url:"<?php echo site_url('document_items/upload') ?>",
    fileName:"myfile"
    });
});
</script>

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

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