0
<?PHP 
$result = mysql_query($query);
$finalval=0;
while($row = mysql_fetch_array($result))
{
$finalval=$finalval."<a onClick='showContent("please display")' 
href='#' >".  $row['Title']   ."</a> <br>" ;
}
echo  $finalval;
 ?>

    <script language="javascript" type="text/javascript"  >

function showContent(value)
{
alert value;

}
</script>

Guys please help me out and please please tell me in a php code on clicking link how to call javascript/ajax function..Thank you so much

3 Answers 3

2

try alert(value) instead of alert value

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

2 Comments

heyya its working fine..:) thanks a ton..but what if i want to pass row id like $finalval=$finalval. "<a href='#' onClick='showContent(. $row['ID'] .)'>". $row['Title'] . "</a> <br>" ;
Please remember to put them in quotes so that its taken as a string for eg : 'showContent(". $row['ID'] .")'
0

Try this one ,opening double quotes within double quotes you need to escape them \

$finalval=$finalval."<a onClick='showContent(\"please display\")' 
href='#' >".  $row['Title']   ."</a> <br>" ;
}

2 Comments

sorry to say but it dint work :/ though there is no error now but still the text is not displayed ..please help me
@InnocentHeart any error you got ? have you tried alert(value) in your js function
0
    <?PHP
        $result = mysql_query($query);
        $finalval = 0;
        while ($row = mysql_fetch_array($result)) {
             $finalval = $finalval."<a class='show-content' data-value='please display' href='#' >".  $row['Title']   ."</a> <br>" ;
        }
        echo $finalval;
    ?>

With javascript querySelectorAll get all a links with class selector then handle them

     <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-value'));
           };
        }
       function showContent(value){
           alert(value);
       }
     </script>

with jQuery it is pretty stright forward,

   <script language="javascript" type="text/javascript"  >
      $(document).ready(function(){
         $("a.content").on('click', function(){
              alert($(this).data('value')); 
              // or make a specific function call
         }); 
      });
  </script>

Hope this will help you, and do not forget to escape the special charactors as mentioned by M Khalid Junaid

2 Comments

thank you so much for your response but i usually get confused with jquery so i avoid using it..and yep my problem is solved will you pls try to answer this stackoverflow.com/questions/20582216/… thank you :)
You can change echo <a class="show-content" data-value="'.$row['ID'].'" href="#">'.$row['Title'] .'</a>'; then manipulate with showContent(id) function from javascript.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.