douxiong0668 2013-10-16 15:25 采纳率: 100%
浏览 30

如何在PHP while循环中通过jQuery运行附加数据?

ill try my best to explain my issue very clearly, ive been working with ajax and im having some troubles appending data inside a while loop using jquery.

this is my loop that displays the title of the post and checks if the title has any comments. if non, it will just show no comments

<ul class="title">
<?php $result = getTitles();
      while ($row = $result->fetch_assoc()) { ?>
      <li> <?php echo $row['title']; ?>  </li>
        <ul class="comments">
           <?php $comments = getComments();
                while (...) { ?>
           <li> <?php //get comments associated to the title posts ?>
           <?php } ?>
        <input box>
        </ul>
<?php  } ?>
</ul>

so it displays like this

title 1
  |- comment 1
  |- comment 2
  |- <input> box

title 2
  |- comment 1
  |- <input> box

title 3
  |- <b>no comment </b>
  |- <input> box

then i have this jQuery that fetches the value from a <textarea id="title"> and append the result into <ul class="title">

/* AJAX - submit status */
$(function() {
    $(document).on('click','.submit', function () {
        var title= $('#title').val().trim();
        if (title.length == 0 ) { //some code } 

        $.post('insert_post.php', {title: title});
        $("<li>" + title + "</li>").prependTo('.title');

    });
});

currently, when i append the data, it justs posts the title without running it inside my while loop.

the way i wanted it to happen is, to run the appended data inside the loop, so when being displayed, it will included all the necessary elements associated to my while loop

one of the elements are the <input> box, each title has their own <input> box. in the case of appended data via jQuery, it only posts the title and not the elements that must be included for every title

  • 写回答

1条回答 默认 最新

  • dongni3854 2013-10-16 15:37
    关注

    Your HTML is invalid — the only valid children of a <ul> element is <li>, and not other elements. Reformat your HTML as follow:

    <ul class="title">
    <?php $result = getTitles();
        while ($row = $result->fetch_assoc()) { ?>
        <li title="<?php echo $row['title']; ?>">
            <?php echo $row['title']; ?>
            <ul class="comments">
            <?php $comments = getComments();
                while (...) { ?>
                <li> <?php //get comments associated to the title posts ?>
            <?php } ?>
                <li>
                    <form>
                        <textarea></textarea>
                        <input type="submit" />
                    </form>
                </li>
            </ul>
        </li>
    <?php  } ?>
    </ul>
    

    Your DOM will look like this:

    • Title 1
      • Comment 1
      • Comment 2
      • Input
    • Title 2
      • Comment 1
      • Input

    In order to fetch the title when you are doing an AJAX call, simply get the title attribute of the parent of the <ul class="comments"> element:

    $(function() {
        $(document).on('click','.submit', function () {
        // Alternate:
        // $(document).on('submit', '.comments form', function() {
            // Travel up DOM tree to get the title, from the <li> element with the title attribute specified
            var title = $(this).parents("li[title]").attr("title");
    
            // Do rest of the AJAX call
        });
    });
    

    As attached here is a barebones JSFiddle demo that you can travel up the DOM tree to search for the right title: http://jsfiddle.net/teddyrised/jyMYY/

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持