简体   繁体   中英

Return multiple echo as data for an Ajax Request

I have an Ajax Request which makes a call to "get_suggestion_table.php". This php file's job is to create a HTML table body. The prettiest way to create this table is to use multiple echo lines, because I have an foreach of every <tr></tr> element. Unfortunately the ajax success function seeems to use only the very first echo of my php file.

Thus I am wondering how I should return my whole table properly. Is the only way returning everything with one echo? (Aka building one big string which includes the whole table I am creating in the php file and then echo this string?).

My get_suggestion_table.php:

echo "<tbody>";
foreach($suggestedSentences as $suggestion)
{
?>
<tr lang="de" dir="ltr" id="sugg-cont-<?echo $suggestion["sugg_id"];?>">
      <td id="sugg-block-<?echo $suggestion["sugg_id"];?>" <?if($suggestion["sugg_id"] == $sugg_id) echo 'class="mytranslation"';?>>
        <div class="actionbuttons">

         <ul class="actions compact">
               <li id="approve-sugg-<?echo $suggestion["sugg_id"];?>" class="alert-success">
                 <a class="vote up" data-original-title="Approve" href="javascript:void(0);" onclick="send_vote(this, '<?echo $suggestion["sugg_id"];?>', 1)">
                <span>Up</span></a>
               </li>
               <li><a class="vote down" data-original-title="Reject" href="javascript:void(0);" onclick="send_vote(this, '<?echo $suggestion["sugg_id"];?>', -1)"><span>Down</span></a>
               </li>
             </ul>
        </div>

            <div class="translation suggestions_compact-suggestion_text" lang="de" dir="ltr"><?echo $suggestion["message"];?></div>

      </td>
    </tr>
<?}
echo "</tbody>";

My Ajax Request:

        // Replace suggestiontable
        var get_suggestion_table = $.post( "/assets/get_suggestion_table.php", { suggestion_id:  sugg_id, original_id: unit_id });

        get_suggestion_table.done(function (data) {
            $(replace_node).html(data);
        });

Try adding a datatype of html:

  $.post( "/assets/get_suggestion_table.php", { suggestion_id:  sugg_id, original_id: unit_id },function (data) {
                $(replace_node).html(data);
            },'html');

See: http://www.w3schools.com/jquery/ajax_post.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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