Jump to content

Complex HTML as a string in JS?


DWilliams

Recommended Posts

There's about 3 or 4 different forums I could post this in, but I guess I'll pick this one.

 

Long story short I have an AJAX call that returns some code to be eval'd. In this case, it needs to update the report container div on my page with the results of the AJAX call.

 

I'm not very experienced with JavaScript but this should be a simple enough task. When I set my AJAX function up to return

document.getElementById('reportarea').innerHTML = 'test';

Then it works just fine. If I replace "test" with my report's HTML, nothing happens (presumably JS is erroring out on it). Now, I just figured this was a case of not escaping quotes or something, but I ran my report HTML through addslashes() and the same thing happens.

 

The HTML the AJAX function returns is unpredictable and can contain all sorts of wonky stuff like linebreaks, semicolons, quotes, brackets, and whatever else. Currently this is a sample of what it's trying to return that does not work:

 

document.getElementById('reportarea').innerHTML = '<b>Client:</b>CLIENTNAME<br />
<b>Date Range:</b> <br /><br />

<table border=\"0\" cellpadding=\"4\" cellspacing=\"0\">
<tr>
<th>Listed</th><th>Acct #</th><th>Name</th></tr>
<tr>
<td>11/16/2009</td><td>AE1000</td><td>JOHN DOE</td><td>1600.00</td></tr>
<tr>
<td>11/16/2009</td><td>AE1000</td><td>JOHN DOE</td><td>1600.00</td><td>11/16/2009</td><td>AE 1001</td><td>JANE DOE</td><td>700.00</td></tr>
</table>
<br /><br />
<b>Number of Accounts:</b> 2<br />
<b>Total Placement Amount:</b> 2300<br /><br />
<b>NOTE: Interest, if assigned, will accrue on the above account(s).
<br />
Thank you for choosing OURCOMPANY for your collection needs!</b>

';

 

What do I actually need to do to prepare this HTML to be acceptable to JavaScript?

Link to comment
https://forums.phpfreaks.com/topic/228422-complex-html-as-a-string-in-js/
Share on other sites

JavaScript doesn't do multi-line strings. It would have to look like

'Client:CLIENTNAME
\nDate Range: 

\n\n...'

 

Tip: use json_encode.

Client:CLIENTNAME

Date Range: 



...'; ?>

document.getElementById('reportarea').innerHTML = ;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.