Send POST Textarea one value per line

Asked

Viewed 2,776 times

1

I have a form with a textarea where it will have a value per line. Example:

inserir a descrição da imagem aqui

I need to send this value to the Mysql database.

Then on another page I need to create a For to list these values.

What brings me to confusion is that there can be nothing, as a reference to the line break..

3 answers

3


Textarea already breaks the text with " n";

<?
  $linhas = split("\n", $txtBox);
  foreach($linhas as $linha)
  {
    echo $linha . " <br> ";
  }
?>


<form action="" method="get">
<p>
  <textarea name="txtBox" cols="50" rows="10" id="txtBox"></textarea>

</p>
<label>
<input type="submit" name="Submit" value="Submit" />
   </label>
</form>
  • So if I already do $_POST for Mysql, it already goes with the broken line and then to do the Just use your code above?

  • Exactly. If you record the $_POST directly in a text field, or varchar, while recovering can use the above code, do the test.

  • 1

    split is obsolete and should not be used in new projects, prefer explode

3

When you retrieve this information, try using php explode to create an array and do the for.

Example

<?php

    $retorno_db = $_POST['text_area']; // Aqui coloca o valor do textarea que vai para o banco

    $dados = explode("\n",$retorno_db);

    for($i = 0; $i < count($dados); $i++) {

        $linha = $retorno_db[$i];
        //Cada vez que passar vai ser uma linha... Agora é só usar a lógica.
        echo $linha."<br />";
    }

0

There is the character ' n' that delimits the line break in Javascript... Try going through the value of the textarea, character by character... when you find a ' n' or the end of the string, you have found the end of a line and can store it in an array. See the code example:

<script>

    var texto = "Texto que \nestará no \ntextarea em várias \nlinhas";

    var linhas = [];
    var linha = ""; 
    for (var i = 0; i < texto.length; i++ ){
       if (texto[i] != '\n') {
          linha += texto[i];
       }

       if (texto[i] == '\n' || i == texto.length - 1) {
          if (linha.length != 0 )
             linhas.push( linha );
          linha = "";
       }       
    }

    //A variável linhas é um vetor contendo cada linha do texto separada
    console.log (linhas);

</script>
  • 1

    It has to be PHP.

Browser other questions tagged

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