0
$finalval=0;
while($row = mysql_fetch_array($result))
{
 $finalval=$finalval. "<a href='#' 
 onClick='showContent(".json_encode($row['ID']).")'>".  $row['Title'] . "</a> <br>" ;

}
echo  $finalval;

<script language="javascript" type="text/javascript"  >
 function showContent(value)
 {
 alert(value);
 }
</script>

Please rectify my error alert box is displaying null instead of ID value.Thanks

3 Answers 3

1

When you call showContent(), you have to put the ID you want to show between ", like this:

$finalval=$finalval. "<a href='#' onClick='showContent(\"".json_encode($row['ID'])."\");'>".  $row['Title'] . "</a> <br>" 
Sign up to request clarification or add additional context in comments.

1 Comment

naaah not working ..Notice: Undefined index: ID alert displaying null :/
0

You don't need to Json_encode just

'showContent(".$row['ID'].")'

I would also suggest you to check your source code in your browser if this doesnt work and see what it prints in showcontent(). I think you are not getting any value in $row['ID']. Again check source code in your browser by right clicking in browser and clicking view source code and check there your showcontent() function and see what value is there

4 Comments

Its showing null even in source code if using json else its like showContent()
Then it probably means that $row['id'] is not giving any value. ok now try echo $row['id'] and check if it prints you id on top of your page.
yep id is displaying using echo $row['ID']
and the problem is solved...thank you soooo much ^_^
0

Try this one

<?PHP
    $result = mysql_query($query);
    $finalval = 0;
    while ($row = mysql_fetch_array($result)) {
         $finalval = $finalval."<a class='show-content' data-id='". $row['ID'] ."' href='#' >".  $row['Title']   ."</a> <br>" ;
    }
    echo $finalval;
?>

<script language="javascript" type="text/javascript"  >
    var element = document.querySelectorAll(".show-content");
    for (var link in element) {
       element[link].onclick = function() {
             showContent(this.getAttribute('data-id'));
       };
    }
   function showContent(value){
       alert(value);
   }
 </script>

1 Comment

jsFiddle demo , Can you use var_dump($row) and post the first result set as sample, so that I can understand what is in $row['ID'].

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.