I have a a page where I am getting data from a SQL database via a call through jQuery and Ajax. When the page loads, the script is getting the data from the PHP file. Ajax is getting the correct response (I can see that in Firebug), but is not displaying it in the div as HTML.
Frontpage:
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<?php foreach($news as $item): ?>
<li style="border-top: 5px solid #a1cb2f;" class="block" id="post_<?php echo $item['date_added'] ?>">
<img class="post_user_img" src="<?php echo "/userdata/profile_pics/".$item['user_img'] ?>">
<span class="feedtext">
<div class="added_by"></div>
<script type="text/javascript">
function get_userdata() {
user_id = "<?php echo $item['added_by'] ?>";
var datastring = 'user_id='+ user_id;
$.ajax({
type: "POST",
url: "/get_userdata.php",
data: datastring,
dataType: "html",
success: function(data){
$('.added_by').html(data);
}
})
};
get_userdata();
</script>
</br>
</br>
<?php echo $item['body'] ?>
</span>
</br>
</li>
PHP:
<?php include("includes/session.php"); include("includes/connect_to_mysql.php");
if(isset($_POST['user_id'])){
$user_id = $_POST['user_id'];
$sql = mysql_query("SELECT * FROM members WHERE id='$user_id'");
$news = array();
$row = mysql_fetch_assoc($sql);
}
echo "<a href='user_profile.php?u=".$row['id'].">".$row['firstname']."".$row['lastname']. "</a>";
?>
Have tried searching a lot of places now and I have looked through a lot of questions here on Stackoverflow, but I was not able to find the answers. If there are any other questions that are exactly the same, please lead me in that direction. That would be very nice of you.