简体   繁体   English

Laravel + Ajax,将数据呈现到html的正确方法

[英]Laravel + Ajax, proper way to present data to html

I am trying to better understand how to correctly/properly present the data from my database to my page. 我试图更好地了解如何正确/正确地将数据库中的数据呈现给页面。 I currently have working AJAX, I can post and retrieve it. 我目前正在使用AJAX,可以发布和检索它。 What I don't understand is how to show it on the page as I need. 我不明白的是如何根据需要在页面上显示它。 IE: IE:

I can show it using <div id="note"></div> and it will show my note body. 我可以使用<div id="note"></div>显示它,它会显示我的笔记正文。 but, how do I get it to show, for instance if I want 但是,如何显示它,例如,如果我想

<div id="note"></div>
<p>By: {{show firstname}} {{show lastname}}</p>

because if I use <p id="firstname">By: </p> that would overwrite the By: text yes? 因为如果我使用<p id="firstname">By: </p>会覆盖By:文本,是吗?

This is my AJAX Get script 这是我的AJAX获取脚本

<script type="text/javascript">
$(function() {
    var note_id = {{$patient->patient_notes->id}}
    $.ajax({
        method: 'GET',
        url: "/patientnotes/" + note_id,
        success: function(data){
            console.log(data);
            $('#note').html(data.note);
            $('#firstname').html(data.firstname);
            $('#lastname').html(data.lastname);
        }
    });
});

This is my JSON data response 这是我的JSON数据响应

{"id":6,"user_id":6,"patient_id":2,"note":"My test note!","firstname":"John","lastname":"Doe"}

and to go along with that, I will also need to pass data (not using the above, another page I have) to my php foreach, how would someone go about getting the AJAX results to my php foreach()? 并且与此同时,我还需要将数据(不使用上面的内容,我拥有的另一页)传递给我的php foreach,有人将如何将AJAX结果传递给我的php foreach()? without looping through it in javascript? 在JavaScript中没有循环通过?

You can use span : 您可以使用span:

<div id="note"></div>
<p>By: <span id="firstname">{{show firstname}}</span> <span id="lastname">{{show lastname}}</span></p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM