Using <pre> Inside a PHP TAG of an html value

Asked

Viewed 668 times

0

Good night, you guys.

One more trick I’m in doubt in my system. I’ve done everything at the beginning of the page with PHP to do a search in my database. But there is a field that I am bringing through the mysqli_fetch_array that I need to bring in the original formatting that was typed in a textarea of a form.

Ai at the time of bringing I created this line in my HTML (But it went wrong):

<input type="text" id="texto-os" style="height: 150px;" name="texto_os" class="form-control" placeholder="Descreva o Serviço" value="<?php echo '<pre>'.$dados_os['ordem_servico'].'</pre>'?>

The result of this is: (All in one row (even though it was recorded with line breaks, and showing the tag as if it had not been running them.

<pre>Texto que vem do Banco de Dados</pre>

I even switched to <input type="text>"because when I was in <textarea> did not show my Database content.

Anyway, in other codes I use the tag after echo to bring the original formatting, but this usually I did at once in the file only with PHP.

This time for practicality, I’m bringing field to field the answers to fill out my form since I still don’t know how to do it via Javascript.

I’m doing wrong or really have no way to use pre inside the html tag value?

Hugs

  • You really need to put the <pre></pre> within the <textarea> ? I tested it here and it didn’t work, I don’t think I can.

  • So, is that this field will be a Service Order Summary. So one will type a lot in a few broken lines. Sure. Then to be better to visualize, I will need to treat this text in some way.

2 answers

1


The <input type="text"> accepts only one line, and there is no way to add HTML within value:

<input type="text" value="<pre>olá

Mundo</pre>">

And <br> it won’t work either:

<input type="text" value="olá<br>Mundo">

That’s not how HTML works.

What you should do is change input=text for textarea, thus:

<textarea rows="10" cols="30">olá

Mundo</textarea>

  • That is, in case I returned to use the textarea and put my php command between opening and closing the tag, ai using nl2br and strip_tags was perfect.

  • @Rafael has no sense to use nl2br and then use strip_tags, that is extremely redundant. It would be better to just use direct strip_tags, in case you have HTML content in the mysql result, if you don’t even have strip_tags makes sense, I need to be honest but I think you are confusing what is happening.

  • 1

    True, I had tried without it, but had left a typo that gave me error. I did not know that only in returning the text would come already with the initial formatting. Thanks for the tip, helped my dear.

-1

To bring the result of the bank with line break use the function nl2br.

In your case it looks like this:

<input type="text" id="texto-os" style="height: 150px;" name="texto_os" class="form-control" placeholder="Descreva o Serviço" value="<?php echo strip_tags(nl2br($dados_os['ordem_servico']))?> 
  • Oh indeed, I didn’t remember nl2br, but it brings my code showing <br /> where should I break the line. Will I have to treat this with javascript? It looks like this. Computador com virus.<br />Não abre Navegador.

  • I edited the answer, now I set the function strip_tags also, see if it works.

  • Oh cool, that works. But not within the value. What I did then was take <input type="text"> and use the <textarea>. Staying now like this: <textarea id="texto-os" style="height: 150px;" name="texto_os" class="form-control" placeholder="Descreva o Serviço"><?php echo strip_tags(nl2br($dados_os['ordem_servico']))?></textarea>. It gave right now. Thanks really Leandro, helped a lot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.