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 9007837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:47:49+00:00 2026-06-16T01:47:49+00:00

I have dynamic content which I generate with PHP, and i want to store

  • 0

I have dynamic content which I generate with PHP, and i want to store some various text in data attribute. Bu the problem is that i need to escape various characters that could damage my entire html

So I need to do some kind of encoding with php and then decode that string with javascript when i want to use that data, what would be the best way to do that?

And just to clarify i do not want to use any inputs or anything like that, i need to store this data in various elements for example a or div and so on…

  • 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-06-16T01:47:50+00:00Added an answer on June 16, 2026 at 1:47 am

    If all you want is to pass some data to JavaScript, why not just write it directly to a variable:

    <html>
    <head>
    <script>
    var data = <?php echo json_encode($data); ?>
    </script>
    <!-- etc. -->
    

    Anyway, if you want to do it your way, you can use htmlspecialchars to escape arbitrary data before putting it in an HTML attribute:

    <div data="<?php echo htmlspecialchars('"\'!)(@UI#_!)(@JP_!()@J'); ?>">Example</div>
    

    If you then read that with JavaScript, it should understand the HTML encoding, so you won’t need to do anything special to read it.

    If your data is more complex, but not binary, you could JSON encode it, then use htmlspecialchars:

    <?php
    $data = array();
    $data["a"] = 123;
    $data["b"] = "example";
    $jsonData = json_encode($data);
    $escapedJsonData = htmlspecialchars($jsonData);
    echo "<div data=\"$escapedJsonData\">example</div>";
    ?>
    

    You can then read that data in JavaScript using JSON.parse:

    var yourDiv = getDivSomehow();
    var data = JSON.parse(yourDiv.getAttribute("data"));
    

    If your data is binary, you can base64 encode it:

    <?php
    $data = someImage();
    $encodedData = base64_encode($data);
    echo "<div data=\"$encodedData\">example</div>";
    ?>
    

    Presumably there’s some way to decode this in JavaScript.

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

Sidebar

Related Questions

I have a dynamic text file that picks content from a database according to
I have this table where I can generate dynamic rows (which has input <type=text
I have a file named graph.php on my server that reads dynamic content from
I have a scenario that, I am creating dynamic html content and I need
I have dynamic listview on my android client app that receive data from remote
I have some divs that have dynamic heights controlled by a 'click' function as
I have some divs that have dynamic heights controlled by a 'click' function as
Starting with a .csproj which defines various xml Content files. Have code generation Target
Using pdftk I generate some dynamic temporary pdf files, which then Django serves to
I have a page that pulls an external JavaScript file, which then generates some

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.