This question already has an answer here:
- PHP error in .xml file? 3 answers
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>