Jump to content

Recommended Posts

Ok, so I have a few ajax pieces on my site, and I am trying to include functionality when something is clicked. Basically, lets say like on facebook, when someone makes a comment, then that new comment is created using something like

 

$.ajax({
type: "POST",
url: "../include/comment_ajax.php",
data: dataString,
cache: false,
success: function(html){
$("#Loadnewcomment"+Id).append(html);
$(".oldcommenthide"+Id).hide(), $(".newcomment"+Id).show();
}
});

 

 

Now in that comment_ajax.php let's say there is another like or comment input field, something like.

 


<div class=\"Likecomment\">
                                <div id=\"likehide\" class=\"likehide$commentID$CommentIDLike\">
                                    <form method=\"post\" name=\"$commentID$CommentIDLike\" action=\"\">
                                    <input type=\"hidden\" id=\"PostID$commentID$CommentIDLike\" value=\"$PostID\" />
                                    <input type=\"hidden\" id=\"UserPostID$commentID$CommentIDLike\" value=\"$PostUserID\" />
                                    <input type=\"submit\" class=\"likepost\" id=\"$commentID$CommentIDLike\" value=\"Like\" />
                                   </form>
                                   </div>";

 

 

How would I get this form to also use the ajax that is on the original page, because this is loaded in thru the ajax, so it doesn't know to use the normal ajax. My likes work perfectly, unless I create a new comment, then on that new comment, if I click "Like" on a comment, rather than it just changing, it reloads the page, then I can click like on it like normal and it changes using my ajax, but not until the page is reloaded.

i just started ajax like two days ago so beware haha.

 

i notice you said you click like and it refreshes the page? right there sounds like something is wrong.

is the like link or button sharing the same id of something else on the page? cause a button made for ajax shouldnt be refreshing the page.

 

i notice i had a situation like this and a link i was clicking on was sharing the same id as a button

But I can select them and view the source and they have unique ID's that I placed in the areas like the "Like". But yeah I have ran into that too with the id's being the same so they refresh, but with these I put new ID's in each id area using the ID from the databse, it just seems like anything I pull in from an ajax statement isn't useful as a <form> at that point if it's bult dynamically thru the ajax and appended or prepended to my other data.

See, here's my return for the exact same pieces with the ID's pulling in, as you can see, each one is unique, and the first one won't work after it is pulled in thru the original ajax request, it just reloads the page once first, then after the page is reloaded and it has the same exact code as before, then it works, but it's like it doesn't do the ajax piece when it's pulled from the url: "../include_wall_front/likepost_ajax.php", because the document can't be ready I guess thru the DOM "$(document).ready(function()" since this is loaded thru ajax? Do you see any difference in Like80 to Like79? Because Like 79 works, but Like80 refreshes, then it works after the refresh, but if I make another Post, then it would ad another Like - Like81 but that one wouldn't work until it's reloaded. Does that make sence?

<div class="Like">
                                  <div id="Like80" class="flash_load"></div>
                                    <div id="loadLike80"></div>
                                   <div id="likehide" class="likehide80">
                                    <form method="post" name="80" action="">
                                    <input id="PostID80" value="80" type="hidden">
                                    <input id="UserPostID80" value="1" type="hidden">
                                    <input id="Friend80" value="0" type="hidden">
                                    <input class="likepost" id="80" value="Like80" type="submit">
                                   </form>
                                   </div>
                                   <div class="unlikehide80" style="display: none;">
                                   <form method="post" name="80" action="">
                                    <input id="PostID80" value="80" type="hidden">
                                    <input id="Friend80" value="0" type="hidden">
                                    <input id="UserPostID80" value="1" type="hidden">
                                    <input class="unlikepost" id="80" value="Unlike" type="submit">
                                   </form>
                                   </div> 
                                 </div>

                              <div class="Like">
                                  <div id="Like79" class="flash_load"></div>
                                    <div id="loadLike79"></div>
                                   <div id="likehide" class="likehide79">
                                    <form method="post" name="79" action="">
                                    <input id="PostID79" value="79" type="hidden"><input id="Friend79" value="0" type="hidden"><input id="UserPostID79" value="1" type="hidden">
                                    <input class="likepost" id="79" value="Like79" type="submit">
                                   </form>
                                   </div><div class="unlikehide79" style="display: none;">
                                   <form method="post" name="79" action=""><input id="Friend79" value="0" type="hidden">
                                    <input id="PostID79" value="79" type="hidden">
                                    <input id="UserPostID79" value="1" type="hidden">
                                    <input class="unlikepost" id="79" value="Unlike" type="submit">
                                   </form>
                                   </div> 
                                 </div>

 

and my ajax is

$(document).ready(function()
{
$(".likepost").click(function(){
var element = $(this);
var Id = element.attr("id");


var $PostID = $('#PostID'+Id).val();
var $UserPostID = $('#UserPostID'+Id).val();
var $Friend = $('#Friend'+Id).val();

var dataString = 'PostID=' + $PostID + '&UserPostID=' + $UserPostID + '&Friend=' + $Friend;


$.ajax({
type: "POST",
url: "../include_wall_front/likepost_ajax.php",
data: dataString,
cache: false,
success: function(html){
$("#loadLike"+Id).append(html);
$(".likehide"+Id).hide(), $(".unlikehide"+Id).show();
}
});


return false;
});
});

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.