Line break

Asked

Viewed 2,971 times

0

Good evening, I make an appointment and in my query I can not do break line, the results appear on one side of the other, already have \n, \\n, <br />,<BR> in PHP and it didn’t work... Can someone help me ?

Javascript

function autocomplet() {

    var min_length = 1; // min caracters to display the autocomplete
    var keyword = $('#cfuncionario').val();
    if (keyword.length >= min_length) {
        $.ajax({
            url: "php/cfuncionario.php",
            type: 'POST',
            data: {keyword:keyword},
            success:function(data){
                var availableTags = [data];
            alert(availableTags);
            $("#cfuncionario").autocomplete({
                 source: availableTags

             });

            }
        });
    }

}

PHP

<?php
require 'connect.php';

$keyword = $_POST['keyword'].'%';
$query = "SELECT nome_funcionario FROM funcionarios WHERE nome_funcionario LIKE '$keyword' LIMIT 10";

$result = mysqli_query($connect,$query);

if($result)
 {
  while($row = mysqli_fetch_array($result))
  {
    echo $row['nome_funcionario'].'\n';



  }
 }
?>
  • If you want to break line in html, use <br>, n to skip line in javascript’s Alert window.

  • he does not break the line tbm, he gets more<br>Your> ...

  • Have tried returning JSON in PHP and treating it in Javascript?

  • How could I do ?

  • Using json_encode in PHP and JSON.parse in Javascript. Good luck.

2 answers

0

Maybe the problem is you use '\n' instead of "\n" double-quote.

There’s a small difference between the two.

Another solution is to concatenate with the constant PHP_EOL.

echo $row['nome_funcionario'] . PHP_EOL;

Read:

Difference between single and double quotes in PHP

0


Let’s consider that your PHP code generates the following result:

Kevin T. Sullivan<br />
Nicolash Correia Cunha<br />
Lavinia Azevedo Lima<br />
Isabelle Lima Silva<br />
Kauan Silva Almeida<br />
Victor Costa Carvalho<br />
Sophia Pinto Barbosa<br />
Vitor Goncalves Barros<br />
Daniel Sousa Melo<br />
Davi Gomes Correia<br />

Note: the above names were generated randomly through the Fake Name Generator and any resemblance to reality will be mere coincidence.

We may attempt to redeem these values with Javascript using an asynchronous request as follows:

$(() => {
  $("#name").on("keyup", function () {
    $.ajax({
      url: "https://gist.githubusercontent.com/acwoss/25f02a59086ecc1d11fd2201586420db/raw/1d372b624d4cc2416685c0c438eff75c0912747c/SOpt-200357-response.html",
      type: 'GET',
      success: function(data) {
        $("#name").autocomplete({source: [data]});
      }
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input id="name" name="name" placeholder="Digite seu nome" />

Note: the request code has been minimally changed to work in the context of the example, but the original idea of the question remained.

Note that the result is exactly the HTML code generated by PHP, but it is a raw string, I mean, Javascript won’t know how to treat it. In order for it to interpret the result, you must indicate how this should be done; and the easiest way is to use known formats such as JSON.

$(() => {
  $("#name").on("keyup", function () {
    $.ajax({
      url: "https://gist.githubusercontent.com/acwoss/bba40268dd678cb4ee3b2291cdede7e2/raw/8586099a9451f44d3ef86046023bbe8c8f9ba202/SOpt-200357-response.json",
      type: 'GET',
      dataType: 'json',
      success: function(data) {
        $("#name").autocomplete({source: data});
      }
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<input id="name" name="name" placeholder="Digite seu nome" />

Indicating that the return will be of JSON format, with dataType: 'json', and PHP generating, in fact, a response in this format, the result obtained in Javascript will already be a list of values that can be passed to the Autocomplement plugin.

I am not in a position to create an example here that is close to your question, but I believe that if it is not exactly the code below, it will be something very close.

PHP generating JSON

<?php
require 'connect.php';

$keyword = $_POST['keyword'].'%';
$query = "SELECT nome_funcionario FROM funcionarios WHERE nome_funcionario LIKE '$keyword' LIMIT 10";

$result = mysqli_query($connect, $query);

if($result)
{
  $output = array();

  while($row = mysqli_fetch_array($result))
  {
    $output[] = $row['nome_funcionario'];
  }

  header('Content-Type: application/json');
  echo json_encode($output);
}
?>

Javascript receiving JSON

function autocomplet() {
    var min_length = 1;
    var keyword = $('#cfuncionario').val();

    if (keyword.length >= min_length) {
        $.ajax({
            url: "php/cfuncionario.php",
            type: 'POST',
            data: {keyword: keyword},
            success:function(data){
                console.log(data);
                $("#cfuncionario").autocomplete({
                    source: data
                });
            }
        });
    }
}

Browser other questions tagged

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