1

I have a xml file:

<?xml version="1.0" encoding="UTF-8"?>
<productenv:Envelope>
    <productenv:Body>
        <products>
            <productName>TestProduct</productName>
            <productPrice>50.00</productPrice>
        </products>
    </productenv:Body>
</productenv:Envelope>

I am receiving this file from an external server using PHP. The XML is then passed to a javascript file. Here is what the javascript does with the variable that XML is stored in:

<script type="text/javascript">
  var result = "<?php echo $result; ?>"; // $result is the variable that sorts the xml file
  console.log(result);
</script>  

When I run the code I get this error: Uncaught SyntaxError: Unexpected number. This is because the XML is turned into a string, but the double quotes that surround the 1.0 and UTF-8 are messing up the file.

Is there any way I could make the XML a multiline string that I could parse?

2

1 Answer 1

2

Assuming the $result string doesn't contain any backtick characters, you can try using javascript's template literals:

<script type="text/javascript">
  var result = `<?php echo $result; ?>`; // $result is the variable that sorts the xml file
  console.log(result);
</script>  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.