dpps0715 2017-09-04 08:40
浏览 28
已采纳

在PHP页面上用Javascript创建XML导致'<?' 被解释为PHP [重复]

This question already has an answer here:

I have a form on a .php page, the contents of which I want to put inside an XML document. I'm currently doing this as such:

var data = "<?xml version='1.0' encoding='UTF-8'?>" + 
'<request><reference>' + formfield1 + '</reference>
<location>' + formfield2   + '</location>
//etc

This approach is outlined in this question.

The variables refer to inputs in the form, and as per console.log they correctly get the values from the form.

However, the issue I'm running into, is the <? and ?> tags, which are interpreted as PHP. I've tried escaping like \<\?, but that results in a variable which includes the backslashes, which obviously isn't what I want.

I'm probably missing something silly, but I've searched extensively for a solution, and have not been able to find one.

UPDATE Thanks to WillParky93, I came up with this code, which breaks the variable into small bits and pieces printed onto the page by PHP:

var data =
<?php print("'" . '<' . '?' . 'xml version="1.0" encoding="utf-8"' . '?' . '>' . "'"); ?>;
data += '<request><reference>' + formfield1 + '</reference>
<location>' + formfield2   + '</location>
//etc

This seems to have resolved the issue, and the data variable is now shown correctly by console.log

</div>
  • 写回答

1条回答 默认 最新

  • duanbogan5878 2017-09-04 08:44
    关注

    This is most likely an issue caused by shorthand tags being enabled on your server.
    If you have access to your php.ini file; Remove short_open_tag=On from your php.ini file.

    However, you can also do this in .htaccess by adding the following line:
    php_value short_open_tag 0

    If you have a lot of code on your server using shorthand, it'll break, so opting for a .htaccess solution where you can limit it to 1 location may be a better option for you.

    Another solution ( taken from the duplicate ) would be to change your javascript code to:

    var data = "<?='<'?>?xml version='1.0' encoding='UTF-8'?>" + 
    '<request><reference>' + formfield1 + '</reference>
    <location>' + formfield2   + '</location>
    //etc          
    

    You'll also need to do the same to the closing ?>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染