dporu02280 2014-08-20 08:54
浏览 23

使用javascript确定数组提交按钮,然后使用Ajax输出数据

I have a list of fetched data, and to every data has a form below it, like a comment box form, where other users can leave a message to the specific data.

What I've tried so far is putting the submit button into an array to distinguish it from one to another (don't know if I'm doing this right as this is one of my first attempt in using Javascript/jQuery library and AJAX).

I can submit the data and insert it into the SQL database just by using PHP/MySQL but I wanted to achieve at least the comment-like system of this community, Stackoverflow, wherein once a comment is posted, it would show up right after hitting the button (not by reloading the whole page in order to submit the data into the database).

This is the dynamically post data:

<?php while($loopquery){ ?>
   <div>
      <?php echo $row['data']; ?>
      <div id="flash[]"></div> <!-- NEW POSTED COMMENT SHOULD BE SHOWN HERE -->
      <form action="#" method="POST">
      <input type="text" name="comment[]" id="comment[]">
      <input type="submit" id="submit[]">
      </form>
   </div>
<?php } /* END OF LOOP */ ?>

And once the submit is clicked:

$(function () {
    var submit = document.getElementById('submit');
    for (var a = 0; a < submit.length; a++) {
        submit[a].click(function () {
            var comment = document.getElementById('comment');
            var hiddentaskid = document.getElementById('hiddentaskid');
            var dataString = '&comment='+comment[a]+'&hiddentaskid='+hiddentaskid[a];
            if (comment[a] == '' || hiddentaskid[a] == '') {
                alert('Please Give Valid Details');
            } else {
                var flash = document.getElementById('flash');
                flash[a].show();
                flash[a].fadeIn(400).html('Loading message');
                $.ajax({
                    type: "POST",
                    url: "commentajax.php",
                    data: dataString,
                    cache: false,
                    success: function (html) {
                        $("ol#update").append(html);
                        $("ol#update li:last").fadeIn("slow");
                        flash[a].hide();
                    }
                });
            }
            return false;
        });
    }
});

I've tried using array to distinguish the data from one to another, but it's not working when I try to tweak the script. New posted message/comment should pop-out in the <div ="flash"></div> area (by using AJAX). Can anyone help me how I can achieve my wanted output?

  • 写回答

1条回答 默认 最新

  • douyue3800 2014-08-20 09:50
    关注

    Check out this fiddle. http://jsfiddle.net/7xjqzmqz/

    Updated fiddle Note I'm using Jquery

    It isn't using your code specifically, but hopefully it gets the point across.

    Replace the url field with where you plan to POST to.

    //message is just an object to keep the comment
    var message = {name: 'Gabs00'};
    
    
    $('form').on('submit', function(e){
        //stop page reload
        e.preventDefault();
    
        //Using $(this), target this specific form
        message.comment = $(this).find('.message').val();
        var json = JSON.stringify(message);
    
        //Finding the parent div, to make sure I append only to this forms comment list
        var $parent = $(this).closest('.item-to-comment');
    
        $.ajax({
            type: 'POST',
            url: '/echo/json/',
            data:{json: json},
            success: function(resp){
    
                //Traversing back down the dom
                var $ul = $parent.find('.comments');
    
                $ul.find('li').removeClass('special');
                $ul.append('<li class="special">' + resp.name + ': ' +
                           resp.comment + '</li>');
            }
    
        })
    
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配