0

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.

Mithun Sen
  • 523
  • 5
  • 19
  • 2
    **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin May 06 '14 at 09:55
  • Is the success callback fired? If ya, what returns `alert($('.added_by').length);` inside it? – A. Wolff May 06 '14 at 09:56
  • It returns 3 when I alert it after the `$('.added_by').html(data);` – Kresten Thorndahl May 06 '14 at 10:00
  • And `data` is the expected string? Could you provide online link where we can check your issue? – A. Wolff May 06 '14 at 10:02
  • the link is [novloc.com](http://novloc.com/get_newsfeed.php) – Kresten Thorndahl May 06 '14 at 10:03
  • why you need to declare to function get_userdata N times (3 times in the page example you provided)? – CFML_Developer May 06 '14 at 10:15

1 Answers1

0

It looks like you are not returning valid HTML, try:

echo "<a href='user_profile.php?u=".$row['id']."'>" .$row['firstname']."".$row['lastname']. "</a>";

See the extra ' to close href attribute.

A. Wolff
  • 74,033
  • 9
  • 94
  • 155