Jump to content

Recommended Posts

So I am trying a new approach to my earlier comment area per post debauchery. I have changed my ajax to the following, however I am getting no results at all, I am not getting the comment_inser_msg = 'Make a comment...' var to fill in the .comment_insert input area. And no alerts placed anywhere in the code fire off any alerts, can anyone point me in the correct direction as to what I am missing to get this to fire anything at all on my page? Here's the simple little test page I made with everything stripped out.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" />

<head>
<script src="js/jquery.js" type="text/javascript" charset="utf-8"></script>  

<script type="text/javascript"> 
    $(function(){
    $("document").on('submit', 'form.response', function() {
    var $form = $(this);
    var $wall = $form.closest('.wall');
    $.ajax({
    type: "POST",
    url: "include_wall_front/updatecomment_ajax.php",
    data: $form.serialize(),
    success: function(){
    $("<li>a bunch of code</li>").appendTo('ul', $wall).hide().fadeIn();
    }
    });
    return false;
    });
     
    var comment_insert_msg = 'Make a comment...';
    $('.comment_insert').on('blur', function(){
    if(this.value == ''){
    this.value = comment_insert_msg;
    }
    }).on('focus', function(){
    if(this.value == comment_insert_msg){
    this.value = '';
    }
    }).val(comment_insert_msg);
    )};;  
        </script> 
        
    </head>
      

<body id="thetrue1">
<a href="">test</a>
          <div class="wall">
    <ul>
    ....
    </ul>
    <form class="response" method="post" action="">
    <input type="hidden" name="action" value="commentupdate" />
    <input type="hidden" name="commentviewedID" value="$PostUserID" />
    <input type="hidden" name="commentID" value="$CommentmainID" />
    <input type="hidden" name="user" value="$user" />
    <input type="hidden" name="userID" value="$user_ID" />
    <input type="hidden" name="PostID" value="$PostID" />
    <input type="hidden" name="UserPostID" value="$CommentUserID" />
    <input type="text" class="comment_insert" name="commentinsert" size="100" />
    <input type="submit" value="Go" />
    </form>
    </div>


</body>
</html>

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" />

<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.7.1");
</script>
<script type="text/javascript"> 
    $("document").on('submit', 'form.response', function() {
    var $form = $(this);
    var $wall = $form.closest('.wall');
    $.ajax({
    type: "POST",
    url: "include_wall_front/updatecomment_ajax.php",
    data: $form.serialize(),
    success: function(){
    $("<li>a bunch of code</li>").appendTo('ul', $wall).hide().fadeIn();
    }
    });
    return false;
    });
    
$(document).ready(function(){
    var comment_insert_msg = 'Make a comment...';
    $('.comment_insert').on('blur', function(){
    if(this.value == ''){
    this.value = comment_insert_msg;
    }
    }).on('focus', function(){
    if(this.value == comment_insert_msg){
    this.value = '';
    }
    }).val(comment_insert_msg); 
});
        </script> 
        
    </head>
      

<body id="thetrue1">
<a href="">test</a>
          <div class="wall">
    <ul>
    ....
    </ul>
    <form class="response" method="post" action="">
    <input type="hidden" name="action" value="commentupdate" />
    <input type="hidden" name="commentviewedID" value="$PostUserID" />
    <input type="hidden" name="commentID" value="$CommentmainID" />
    <input type="hidden" name="user" value="$user" />
    <input type="hidden" name="userID" value="$user_ID" />
    <input type="hidden" name="PostID" value="$PostID" />
    <input type="hidden" name="UserPostID" value="$CommentUserID" />
    <input type="text" class="comment_insert" name="commentinsert" size="100" />
    <input type="submit" value="Go" />
    </form>
    </div>


</body>
</html>

Perfect thanks! Got it working, but my closest doesn't work, var $wall = $form.closest('.wall'); I have multiple forms, dynamically created, each with an ID, and when I put any text in any of them, and make this run it puts comments in every ul I have on the page rather than looking at the closest <div class="wall"><ul>, I'm reading more about closest, but I'm kind of lost. It should be appending to the closest ul to this div shouldn't it? Not every ul I have on my page?

Probably need to see the page in action to properly debug it, but if I were to hazard a guess, I would say that the variable you are applying ".closest()" to is actually a collection of elements. So jQuery is basically calling closest() on all the forms on your page. I might be totally wrong here, just guessing.

 

Is this page hosted live where we can have a look?

Yeah, and you seem correct, it seems to be looking at ALL forms on the page because if I try to get the values of any of the hidden inputs, and alert them they come up undefined. like

var $form = $(this);
    var $PostID = $('.formid').val();
        var $wall = $form.closest('.comment');
        var commentviewedID = $('#commentviewedID').val();
        var commentID = $('#commentID').val();
        var user = $('#user').val();
        var userID = $('#userID').val();
     alert (userID);
    $.ajax({
    type: "POST",
    url: "include_wall_front/updatecomment_ajax.php",
    data: $form.serialize(),
    success: function(){
    $("<li style=\"list-style-type: none;\">Code</li>").appendTo('ul', $wall).hide().fadeIn();
    }
    });
    return false;
    });

 

Here's the page

http://www.thetrue1.com/indexajaxtest2.php you'll have to login and can use username:Eric and password:test1234 to get in so it uses a userid, I have the userID alerting on it.

Thanks

Here's a simple page I put together using the code, no login needed, you can see any time you press go on either one, both Wall's ul's get the bunch of code appended to it. The code is in the source. How do I get it to do it one at a time? :(

http://www.thetrue1.com/indexajaxtest3.php

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.