Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 608849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:30:11+00:00 2026-05-13T17:30:11+00:00

I’m building a CMS for a client (Building on top of wordpress). I’m having

  • 0

I’m building a CMS for a client (Building on top of wordpress). I’m having an issue with some data I am returning from my database.

Basically I am building Javascript Objects from PHP data so that I can update certain areas of my site upon mouse click.

example (this would work fine):

<script language = "Javascript>
  var myObject = new Object();

        function updateDiv(id)
        {
           myObject.name = '<?php echo $valueName ?>'; // $varHeadline = "Bob"
           myObject.headline = '<?php echo $value_headline ?>'; // $varHeadline = "My Story"

           var div = document.getElementById(id).innerHTML = myObject.name + 
           '<br>' + myObject.headline';
        }
</script>

The problem comes up when the data I bring back from my database already has some html, or line breaks in it.

Example:

    echo $varHeadline;
    // returns <h1>This is my headline</h1>
   //            This is part of the value too.

So if I create a Javascript Object with that data:

function updateDiv(id)
{
  myObject.headline = '<?php echo $varHeadline; ?>';
  var div = document.getElementById(id).innerHTML = myObject.headline;
}

I get errors in my Javascript.
I would like to continue populating my divs with Javascript Object data, but am unable to on account of some of the data containing HTML (or even single or double quotes for that matter).

I DO want to retain my HTML formatting (the <h1> tags and so forth) so using htmlspecialchars or strip_tags is out of the question. Is there any die hard way of storing returned HTML without killing Javascript?
Thanks in advance.
-J

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T17:30:12+00:00Added an answer on May 13, 2026 at 5:30 pm

    json_encode() the string and take away the quotes:

    myObject.headline = <?php echo json_encode($varHeadline;) ?>;
    

    Of course, you could rewrite this as:

    <script language = "Javascript>
      var myObject = <?php echo json_encode(array('name'=>$valueName,'headline'=>$value_headline)); ?>;
    
            function updateDiv(id)
            {
               var div = document.getElementById(id).innerHTML = myObject.name + 
               '<br>' + myObject.headline';
            }
    </script>
    

    edit: as noted in the comments below, always make sure you can trust the HTML you’re placing on the page. If $valueName or $value_headline is coming from user input, this is a bad idea if you don’t validate the HTML in some other fashion, you’re open to XSS attacks and the like: http://en.wikipedia.org/wiki/Cross-site_scripting

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.