I have a value stored as <b> supposed to be bold </b> in mysql.
while fetching through php I used nearly all html entity related functions but no avail
//will be returned as json for vue
$item ['value'] = $fetched_data;
in vue template I use like this
{{$value}}
and it gives me as html tags where I am supposed to print the bolded text, anything should I do in PHP or Vue to make this happen?
You can use v-html="value" on the element which needs to contain your value.
https://v2.vuejs.org/v2/guide/syntax.html#Raw-HTML
Related
On a PHP+MySQL project, there's a string of text coming from a MySQL table that contains HTML tags but those tags never get rendered by Google Chrome or any browser I tried yet:
You can see that the HTML (p, strong) aren't getting interpreted by the browser.
So the result is:
EDIT: HTML/PHP
<div class="winery_description">
<?php echo $this->winery['description']; ?>
</div>
$this->winery being the array result of the SQL Select.
EDIT 2: I'm the dumbest man in the world, the source contains entities. So the new question is: How do I force entities to be interpreted?
Real source:
Any suggestions? Thanks!
You are probably using innerText or textContent to set the content of your div, which just replace the child nodes of the div with a single text node.
Use innerHTML instead, to have the browser parse the HTML and create the appropriate DOM nodes.
The answer provided by #Paulpro is correct.
Also note that if you are using jQuery, be sure to use the .html() method instead of .text() method:
$('#your_element').html('<h1>This works!</h1>');
$('#another_element').text('<h2>Wrong; you will see the <h2> in the output');
I am saving html data into mysql
like below
<p><div id="aaa">asdadad</div></p>
<p><div id="aaa">asdadad</div></p>
<div class="something">some data</div>
when showing the html as string in front end using php , it coverts all 3 lines(above) to html content not just string
But I expect the below result
first 2 line as string and 3rd line as html
I used:
html_entity_decode($content_from_db, ENT_QUOTES, 'utf-8');
Don't do anything to the data, just print it straight to the browser. If the data is stored as you say then you will get expected behaviour. If you don't get expected behaviour then either:
Data is not stored correctly, view it in MySQL console or a RDBMS
You are post-processing data after it is being pulled from DB
I am using a jquery editor.And the content will be saved in the database along with the html tags . That is for example I have typed "aneesh" in the editor and using editor makes the text bold and italics the saved the content to db .
So the content will be saved in the db along with text and html tags
Now i want take the content from db and want show the text in formated ways that is need to transform the html tags that in my the content want to show like this aneesh Is there any way for this please help me
To store and retrieve html you should use htmlspecialchars() - this preserves html tags etc etc when saving and htmlspecialchars_decode() - this converts them back to proper html that can be displayed - when displaying the retrieved content.
Storing using this and calling the decode function when populating the textarea behind the wysiwig editor will/should have kept everything preserved.
Or you can also use htmlspecialchars()
$a=htmlspecialchars($a);
<text>$a</text>
wud help u save and retrieve the html characters
Just select the data out of database (i'm using PDO): and echo/post the result to the document without string formating:
$sql = 'SELECT * FROM table;';
$stmt = $DB->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['result'];
if you have content with html tags like i-e <b>Hello</b> in the database, and when you return it from the database again, the jquery editor that you are using will automatically remove those tags and will transform your text again and make it i-e bold (if hello in case).
There is no need to do anything but just echo it out.
you need to use javascript for better and easy solution.
get raw html content from db (with php) to show web browser (with javascript): innerHTML or .html() (JQuery)
I have some multi line text saved in MySql database (VARCHAR 255). When i load it, and process it using standard php function "nl2br", it echoes fine (multi line). But, when i load multi line text from database, make it "nl2br" and then send it to javascript (so it gets displayed in textarea), it won't be displayed! What's wrong?
echo "<SCRIPT>FillElements('".$subject."','".$text."');</SCRIPT>";
P.S.
FillElements function:
function FillElements(Sub,Txt)
{
document.getElementById('txtSubject').value=Sub;
document.getElementById('txtMessage').value=Txt;
}
textareas don't actually store the contents in an attribute like value in the same manner as input elements. They actually store the contents in in between the <textarea> and </textarea> tags. Meaning that the contents is actually treated as CDATA in the document.
<textarea>
This is my Content
</textarea>
Produces a text area with "This is my Content" as the contents.
The implication of this is that you cannot use the code you have to alter the contents of a textarea. You have to alter the innerHTML property of the textarea. I have set up a simple example here:
http://jsfiddle.net/wFZWQ/
As an aside, since you are populating the fields using PHP on the creation of the page, why not merely fill the data in the HTML markup, this seems like a long way round to do it.
Also, since you don't appear to be using it, have you seen [jQuery][1] it abstracts alot of things out, so instead of typing document.getElementById("the_id") to get an element you can use CSS selectors and merely write $("#the_id") to get the same element. You also get a load of useful functions that make writing javascript mucxh easier.
[1]: http://jquery.com jQuery
Newline tags (<br />) don't cause actual new lines in <textarea>.
You can pass the "real" newlines (\n) to your <textarea>, though.
I created a fiddle for that.
EDIT: For the updated FillElements code:
$subject = "String\nWith\nMultiple\nLines";
printf('<script type="text/javascript">FillElements(%s)</script>',
json_encode($subject)
);
My guess is that your HTML source code looks like this:
<script>FillElements("foo","foo
bar
baz");<script>
Correct?
In JavaScript, strings cannot span multiple lines...
I have a php page that is returning some data in json. Basically I am doing echo on this page.
The data being returned has some html tags. This is causing my jQuery code to break.
Is there a way to clean up the data and strip off the tags before putting it in the json object?
Furthermore, I am trying to display the data from json into a textarea and ideally I would like to show the html tags in the textarea...
When you create the json, you can use json_encode() on the values that have html in your php code - this will escape the values in json so that it validates and will be usable in jquery. I haven't tried it, but you should not have a problem adding the value to a textarea.