1

I'm trying to use Ajax to refresh a part of the page that shows a small part of my database. My problem is that the result I'm trying to send back is in form of a (long) string and I can't find a way to send a string as an ajax response. I'm a total beginner in ajax and I don't know how to solve this problem.

This is what my ajax looks like :

$.ajax({
            url: "{{ url('/news/lastfive') }}",
            method: 'get',
            success: function(result){
                $('#machin').html(result);
            }
        });

And the php function (I'm working in Laravel so there are some laravel functions) :

public function lastfive(){
        $liste = DB::table('news')->latest()->limit(5)->get();
        $str = <<<HTML
        <table>
......
        <tbody>
HTML;
        foreach ($liste as $elements){
            $str .= '<tr>';
            foreach($elements as $element){
                $str .= <<<HTML
                <td>$element->author</td>
                <td>$element->message</td>
                <td>$element->date</td>
HTML; 
            }
            $str .= '</td>';
        }
        $str .= '</tbody></table>';
        return response()->json(['success'=>$str]);
    }

When I do this my string $str cannot be encoded to json format and it doesn't work.

I also tried this in the php :

public function lastfive(){
        $liste = DB::table('news')->latest()->limit(5)->get();
        $liste = json_encode($liste);
        return response()->json($liste);
}

This time I manage to passe the data onto my html page, but it is much less convenient for the layout and there are problems with the encoding of non ascii characters. Is there a better solution here that I'm missing?

I hope this question is not too stupid ;)

1 Answer 1

1

There's a much easier way to do this, using view(). Save your generated HTML as a .blade.php file:

table.blade.php:

<table>
    <tbody>
        @foreach ($liste as $elements)
        <tr>
            @foreach($elements as $element)
            <td>{{ $element->author }}</td>
            <td>{{ $element->message }}</td>
            <td>{{ $element->date }}</td>
            @endforeach
        </tr>
        @endforeach
    </tbody>
</table>

Then, instead of generating your HTML in PHP, load the file via view():

Controller:

public function lastfive(){
    $liste = DB::table('news')->latest()->limit(5)->get();
    $html = view('table')->with(['liste' => $liste])->render();

    return response()->json(['html' => $html]);
}

This way, you can query your Database, pass the elements to your table.blade.php view, use render() to convert it to a string containing your HTML, and finally return the HTML via response()->json()

Edit:

Your AJAX response should be slightly altered:

$('#machin').html(result.html);

Target result.html to set the element's content.

Sign up to request clarification or add additional context in comments.

3 Comments

That is the way to do it.
Thanks a lot @Tim Lewis ! I did just as you said and it worked perfectly. :) I didn't know the render() method existed, it's very useful !
No problem! I forget where I found that (the documentation for Views is missing it), but yeah, definitely useful if you don't want to manually generate HTML strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.