1

I have a while loop that fetches and echos status updates. I am trying to echo a "REPOST" link during my while loop, so that it appends to any status update (sits below it), and when clicked, passes the ID thru to an external .php file. This link is jquery+ajax.

<script type="text/javascript">
$(document).on('click', 'a[id^=repost]', function(){
    var thePostID = $(this).prop('id').split('-')[1];
    //this now has the id of the $row['id'] we declared earlier.
    $.ajax({
        url: 'reposthandler.php',
        type: 'POST',
        data: {'id' : thePostID},
        success: function(data){  
        }
     });
});
</script>


<?php

include ('time.php');


$query = "SELECT poster, posterpic, category, type, date, id, follow.beingfollowed 
FROM usergen LEFT JOIN follow ON follow.beingfollowed = usergen.poster WHERE   
follow.iwannafollow 
= '*".$loggedin."' ORDER BY usergen.date DESC ";
$result = mysql_query($query) or die(mysql_error()) ;


$rows = array();
while($row = mysql_fetch_array($result)) {
    $row['ago'] = ago($row['date']);
    $rows[] = $row;


    echo '<br />';
    echo '<img src="' . $row['posterpic'] . '" width="40px" height="50px">';
    echo '<font color="red" size=2px><i>';
    echo '<a>'.$row['poster'].'</a></i></font>';
    echo '<br />';
    echo $row['category'] . ' for ' . $row['type'] . ' (' . $row['ago'] . ')';
    echo '<a href="#" id="repost-',$row['id'],'">REPOST</a>';
    echo '<br /><br /><HR size="4" width="auto" color="white">';
}
echo '</div>';
?>

The links are dead and do not link thru to anything. Why is this? Do I need to add something below "success: function(data)"?

1 Answer 1

1
Simply add an element to the loop for the button. 

echo '<a href="#" id="repost-',$row['id'],'"> Repost This </a>';

Bind the click function to a static parent element.

$(document).on('click', 'a[id^=repost]', function(){
    var thePostID = $(this).prop('id').split('-')[1];
    //this now has the id of the $row['id'] we declared earlier.
    $.ajax({
        url: 'path/to/my/controller.ext?postID='+thePostID,
        type: 'POST',
        success: function(){
          //do whatever with the response.
        }
    });
});

Now you're being passed the ID of the post you want to repost, and you can handle it however you want in your back end php file.

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

14 Comments

So, I have implemented your solution, but it seems as though the link "Repost This" does not do anything -- as in it is not grabbing the custom URL that I input where you have 'path/to/my/controller.ext'. What would be causing this?
@Mike inside of the click function, do a console.log(thePostID). If you structured the HTML just as I've shown you above in the echo, then the <a> tag will have an id of repost-137 where 137 represents the ROW ID of the post. The above click function constructs a variable by splitting the ID into an array on the -, then taking the last piece, or [1] in this scenario. The url to your file should really be the only piece missing here.
@Mike If you are sending it to a file called myFile.php within a folder struture of /var/www/mydirectory/somelocation/anotherlocation/myfile.php, then that's the URL. We send over a POST type, so the check for the variable should be if($_POST['postID']): which will then give you access to the variable we're passing over.
Ok, thank you. Any idea why when I click one repost button, ALL the buttons act as if they are being pressed? When I check my database, it's as if each button is being pressed, and each one is posting the correct information. The problem is, I only want one click firing the one button I am clicking! : )
@Mike Nope. The reality is that if you structure your HTML and jQuery right this won't be a problem. Are you by chance trying to do this assignment within a for each or each or while loop? That would cause this type of issue.
|

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.