1

I have below code. when I click reply link, it will show a comment box. I want 2 points. or give me best way to do my work.

  1. When 1 comment box open other must be hide.
  2. when I click on send button correct value should send vie serialize values.

These are the codes PHP code

<?PHP 
    for($i = 1; $i < 10; $i++)
    {
    ?>  
    <div>comments text etc etc...</div>
    <a href="" class="reply-comment"> Reply </a>
    <div class="reply-comment-form" style="display:none;">
        <form class="comment_form" method="post">     
            <textarea name="comment_box" rows="6" class="span10"></textarea> <br /><br />
            <input type="button" onClick="send_comment()" class="btn btn-primary" value="send" />
        </form> 
    </div>
<?PHP 
   } 
?>

Jquery code

<script> 
    $(function(){
        $('.reply-comment').on('click', function(e){ 
            e.preventDefault();
            $(this).next('.reply-comment-form').show();
        });
    });


    function send_comment()
    {   
        $.ajax({   
           type: "POST",
           data : $('.comment_form').serialize(),
           cache: false,  
           url: 'test.php',  
           success: function(data){

           }   
        });   
    }
</script>

test.php file no need. I am checking values through firebug. please help me to clarify this problem. or give me best way to do my work.

I am stuck since 2 days. Thank you for your valuable time.

2 Answers 2

1

For the first one

$('.reply-comment').on('click', function(e){

    e.preventDefault();

    // Hide others
    $(".reply-comment-form").hide();

    // Show the one which is required
    $(this).next('.reply-comment-form').show();

 });

And for second, do a .on("submit"... on the form and it will serialize the right input fields only.

UPDATE:

<form class="comment_form" method="post">     
      <textarea name="comment_box" rows="6" class="span10"></textarea> <br /><br />
      // Change type to submit and remove onclick
      <input type="submit" class="btn btn-primary" value="send" />
</form> 

jQuery:

$(".comment_form").on("submit", function(e){
    e.preventDefault(); // Here
    var _data = $(this).serialize();
    $.ajax({   
        type: "POST",
        data : _data,
        cache: false,  
        url: 'test.php',  
        success: function(data){
            console.log(data);
        }   
    });   

});
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, Now other boxes hide. but forms are working hidden. when I click submit button all form values send. so can you please tell me how can I use onSubmit function or can I disable other textares ?
when I use on submit function page will be reload. I want to do this through ajax, because of after that values retrieve as json. so can you tell me how to prevent that. I want fully ajax function. thanks for your time
Yes you should use e.preventDefault() see updated answer
Hi, I used on("click") instead of on submit. now it is works. but I want to send values when I click on submit button. not click on form
1

I found a solution. @void helped me for this.

    $(".test").on("click", function(){

    var _data = $(this).parent().serialize();
    $.ajax({   
        type: "POST",
        data : _data,
        cache: false,  
        url: 'test.php',  
        success: function(data){

        }   
    }); 

});

Thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.