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