0

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

3
  • 2
    Check if short_open_tag option is enabled and disable it. Or do what that page says to bypass it. Commented Sep 4, 2017 at 8:41
  • @Quentin this question is not a duplicate of that question, as this concerns the inclusion of XML on a PHP page, and that question is the other way around. Also, I tried the solution for that question, and does not resolve my issue. Commented Sep 4, 2017 at 8:52
  • @Tijmen — I can't see any way to interpret your question that doesn't boil down to "I have a .php file with <?xml in it and the <? is triggering PHP mode". That makes it a duplicate. If the solution there didn't fix the problem then show a more complete minimal reproducible example. Commented Sep 4, 2017 at 9:03

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

I don't have access to the PHP.ini file (although I do have access to the person who does), and this server uses NGINX which does not have .htaccess. In addition, this site runs under Wordpress. I might be mistaken, but I was under the impression Wordpress requires short tags to be enabled, so wouldn't this solution break my site entirely?
The standard coding practise is to not use shorthand tags so I would be very suprised if it's being used in wordpress. The thread that marked this one as a duplicate has a good bypass however involving splitting the < away from ?xml
Thanks for trying, I'll see if any other solutions are offered, otherwise I will try yours.
I edited my answer to include a split of < and xml
I used your answer as a starting point. I ended up breaking the variable apart to the extreme, but now it's working. Thanks! I'll update my question in case someone finds this thread in the future.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.