【问题标题】:php foreach -> Get corresponding data with jsphp foreach -> 用js获取对应数据
【发布时间】:2015-11-15 19:57:50
【问题描述】:

我有一个博客并从一些 SQL 数据库中获取数据。 我在从 foreach 循环中获取正确数据时遇到问题,我不知道为什么。我会尽力解释。

对于每张图片,都有一个文本区域,用户可以在其中发表评论。对于每条评论,我必须发送相应的 id (ajax) 以添加到数据库中。

foreach($db->query("SELECT id, image FROM news") as $row)
{           
<a href='images/news/{$row['image']}'><img src='images/news/{$row['image']}'/></a>";

<div class="save_comment">

<table width="400" border="0" cellspacing="1" cellpadding="2">
                <tr>
                    <td>
                    <input type="text" class="_comment_id" name="id" value="<?php print_r($row['id'] );?>"  /> 
                    Comment:
                    <textarea  id="_comment_text" class="comment_area" name="comment_area" maxlength="400" cols="80" rows="8" ></textarea>
                    </td>
                </tr>
                <tr>
                    <td>
                    <a class="post_comment" href=".popupContainer">Post Comment</a>
                    </td>
                </tr>
           </table>

</div>
}

点击“发表评论”后,js-script 将被初始化....

<script type="text/javascript">
jQuery(document).ready(function() {
$(".post_comment").leanModal({top : 200, overlay : 0.6, closeButton: ".modal_close" });
$(function(){

jQuery('.tabs .tab-links a').on('click', function(e)  {

    var currentAttrValue = jQuery(this).attr('href');
    jQuery('.tabs ' + currentAttrValue).show().siblings().hide();
    jQuery(this).parent('li').addClass('active').siblings().removeClass('active');
    e.preventDefault();
   });
 });
}); 
</script>

....并显示一个弹出容器(类 .popupContainer 的 div),用户最终可以在其中提交评论。

<div class="popupContainer" style="display:none;">
    Your Name: <input type="text" class="_user_name" />
    Email:<input type="text" class="_comment_email" />
    <input type='button' class='_submit_comment' value='Speichern' >
</div>

通过提交此评论,以下脚本被初始化,数据将通过 ajax 发送。

<script type="text/javascript">
$("._submit_comment").click(function(){

var parentObj = $(this).closest('.save_comment');
var _comment_id = parentObj.find('._comment_id').val(); -> I don't get the correct id

所以问题就从这里开始了。我只从 foreach 循环中获取第一个 id,但我需要为每条评论获取相应的 id。任何人都可以帮助解决这个问题吗? 谢谢 混蛋

【问题讨论】:

  • 谁能帮我解决这个问题,或者告诉我哪里出了问题(逻辑)?

标签: php jquery


【解决方案1】:

这部分是错误的恕我直言:

$(this).closest('.save_comment');

您应该在单击“发表评论”时设置一个 JS 变量,其 id 可以存储在链接中,例如:

<a class="post_comment" href=".popupContainer" data-idcomment="<?php print($row['id'] );?>">Post Comment</a>

还有

var _comment_id = null;
jQuery('.tabs .tab-links a').on('click', function(e)  {
    _comment_id = jQuery(this).data('idcomment'); 
    var currentAttrValue = jQuery(this).attr('href');
    jQuery('.tabs ' + currentAttrValue).show().siblings().hide();
    jQuery(this).parent('li').addClass('active').siblings().removeClass('active');
    e.preventDefault();
   });
 });
}); 

然后您可以使用_comment_id 进行ajax 调用

【讨论】:

  • 感谢您的回答。我简化了我的代码并删除了部分 js-script。现在只调用popup Container,用户可以在其中输入文本并同时提交评论。
猜你喜欢
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 2016-10-31
  • 2014-10-16
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多