Break lines in a text that comes from the database in a single variable

Asked

Viewed 148 times

1

I’m having difficulty inserting line breaks in the text that comes as a response from a select in the database through an ajax request.

$('select[name=tipo]').change(function() {

    var value = $(this).val();
    var _token = $('input[name="_token"]').val();
    $.ajax({
        url: "{{ route('tipo.getTemplate') }}",
        method: "get",
        data: {
            value: value,
            _token: _token
        },
        success: function(result) {
            console.log(result)
            $("#template").val(result[0]['conteudo']); //este é o resultado onde quero quebrar linhas

        }
    })
});

<div class="form-group col-sm-12">
    <label class="control-label" >{{ trans('app.email')}}<span class="spancolor">*</span> </label>
    <textarea style="height: 200px;" value="" class="form-control" id="template" name="template" placeholder="{{ trans('app.template')}}"></textarea>
</div>

public function getTemplate(Request $request) {
    $value = $request->get('value');

    $dadosTemplate = DB::table('template')
            ->select('template.conteudo', 'template.observacao', 'tipoincidente.observacaoincidente')
            ->join('tipoincidente', 'tipoincidente.id', '=', 'template.idtipoIncidente')
            ->where('tipoincidente.nome', $value)
            ->get();


    return response()->json($dadosTemplate);
}

Here is the email template, in which I send the text you have in the textarea. This text is in the {{$email variable}}.

<div class="fonte">
        <p>Prezados Senhores,</p>

        <p>{!!$email!!}</p>

        <p>{{$finalizacaodoemail}}</p>


        </div>

Code for sending email

$emails = ["[email protected]", "[email protected]"];
                $copias = ["[email protected]"];


            $beautyemail = app()->make(\Snowfire\Beautymail\Beautymail::class);

        $beautyemail->send('dashboard.templateemail', ['email' => $template, 'first_name' => $first_name, 'last_name' => $last_name, 'cargo' => $cargo, 'phone' => $phone, 'ramal' => $ramal, 'finalizacaodoemail' => $finalizacaodoemail],  function($m) use ($emails,  $assunto, $copias, $template, $finalizacaodoemail)
            {    
                $m->to($emails)->subject($assunto)->from("[email protected]", "Lorena Dutra")->cc($copias, "email cópia");    
            });
  • Why exactly do you want to insert this line break? Sometimes there is an easier way to achieve the same goal. Are you having problems with text placement in some div?

  • Because the text that comes in the textarea is sent by email, I will update the question by adding the email template that I am using. When sent by email the text goes all together without line breaks, I would like each "." to have a break.

1 answer

1


After the first answer I think I understand your problem. In addition to displaying the formatted text in the element you also want the email to be sent with the formatted content. The best option is to format the content in the back end (PHP code).

I hope this code helps you:

public function getTemplate(Request $request) {
    $value = $request->get('value');

    $dadosTemplate = DB::table('template')
        ->select('template.conteudo', 'template.observacao', 'tipoincidente.observacaoincidente')
        ->join('tipoincidente', 'tipoincidente.id', '=', 'template.idtipoIncidente')
        ->where('tipoincidente.nome', $value)
        ->get()[0]; // Suponho que você esteja usando Laravel, então eu aconselharia a utilizar o método "first()" ver https://laravel.com/docs/5.8/eloquent#retrieving-single-models

    $template = $dadosTemplate[0];
    $template->conteudo = implode(".<br>", explode(". ", $template->conteudo));

    return response()->json($template);
}

In the ajax request I would do so:

success: function(result) {
    console.log(result)
    $("#template").val(result.conteudo);
}

Browser other questions tagged

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