How to retrieve html code as preview among text in sql php - php

I submitted some text to an SQL database like this:
"You can check my photo below (img src=..myimg.jpg..)and please also comment."
Now i want to retrieve it in PHP and containing image preview. The image code is HTML.

You need to escape data from DB before presenting it in page in order to what is stored doesn't mess with your page. But if you want also show the image, then you have to extract the img src part and put it back inside escaped output in proper html tag.

Related

How to format text from the DB before showing on a Website

I have a mysql database that contains fields with paragraphs of text that i want to show on my website. I have a normal textbox where i put text and send to the database, however when i read the data on my website the text is not proparly formatted.
How do i make sure that when i access the data from my website it comes properly formatted. Is there a textbox that saves the data in he mysql database such that when i consume it from the website it gets properly formatted on my website.
I am using html and php on my website.
You need to use a WYSIWYG (What You See Is What You Get).
https://en.wikipedia.org/wiki/WYSIWYG
This will turn a regular form textarea input in to an input that allows formatting.
Take a look at TinyMce and CKEditor.
https://ckeditor.com/
https://www.tinymce.com/

Strip html tags in php for user view before emailing

I am working on a php project which involves jquery and ajax. The data gets processed through ajax call and the message gets displayed in an email dialog box.
I want the email to be html enabled but the thing is that html tags should be invisible for display before hitting send button. Looking for some help. Thanks in advance.
You can display the content using javascripts innerHTML. It will show the content as marked up by the html, not the html tags and plain text.
Have you tried any code yet? It is not clear what you are trying to do, perhaps you wish for the user to be able to edit the email, if so you would need to use a script that supported your needs.
You say the you are displaying the response which you get from the php server. This approach is not so tidy but if you want this might be a simple solution. Use php strip_tags() and create a different response and add it to your response string with an identifiable key. The you have two response values which one can be used to show to the user and the other can be emailed which contains the original html included response.
you can display the response without the html tags to the user.
Your question is unclear. Are you displaying the response of the ajax call in a textarea? If so, then you can't get rid of the html tags because you will lose all the formatting.
If the content needs to be editable then perhaps try an embedded WYSIWYG editor such as TinyMCE.
If the content doesn't need to be editable then simply insert the html into a div instead of a textarea. See https://api.jquery.com/html/
If you simply want to strip tags then the strip_tags() PHP function will do that for you.

How do I display html tag in texts from Database

I am trying to save the data in DB through the user. User can add html markup so some data will be like <em>texts</em> and sometimes <br> tag is added.
I run into a problem when I extract the data out of the DB and display it to the user, I got a blank field for <br> tag instead of showing '<br>'. Is there a way to display <br> tag as a text in my web browser when the user get data from the DB? Thanks a lot!
Always use htmlspecialchars() for escaping user's data output.
You could wrap it in <pre> tags.
The HTML <pre> Element (or HTML Preformatted Text) represents preformatted text. Text within this element is typically displayed in a non-proportional font exactly as it is laid out in the file. Whitespaces inside this element are displayed as typed.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

Dynamically create content from SQL database

So basically all I want to do is a basic image uploading page. When the user fills out a form they submit a file either from their computer or from a URL then the image gets stored on the server.
I want to then display the images on a HTML page. I already have the code to dynamically create the divs that would hold the uploaded content.
function AddTiles() {
var mydiv = document.createElement("div");
mydiv.setAttribute("id", "mydiv");
mydiv.setAttribute("class", "tiles");
mainContent.appendChild(mydiv);
}
But whenever you upload the image the page refreshes, and then the divs would no longer be there to place the image in. So I want to save the image to a database instead, and then on page load, read through the database and generate the html content and then display the images.
So if there were 4 images in your database, then the program would create 4 div tags and put each image in its respective div tag.
Also, I just need the code to read from the database and create the html content on load. I can create the database and store the images with no problem.
If you are going to read and output information stored in a database, you might as well generate the HTML content on the server-side (PHP) without using JavaScript i.e. echo "<img src='$url' />".
You can also read PHP MysQL Select tutorial for more information on retrieving data from your database.

display images on specific keyword php mysql

There is a form. It has text area,a text field and an upload image option.
I want that if the user writes some paragraph and wants the image to be displayed exactly above next paragraph, then he/she write a unique keyword like 'imagehere' in text area as well as in text field.
this unique text would be stored in another column.
Then while retriving the text from database, if there is such keyword then replace it with
<img src="path">
tag
Is this correct way to display images according to position defined by user?
It works!
I wanted to do this to avoid asking the user to write
<img src="photos/uploadedimage">
in textarea.
So if the user writes specific keyword then
by using
str_replace, you can easily replace the specific keyword with
img src tag
and these images are displayed.

Categories