0

I have a php file that shows all the user details in table form. On click of edit, I am want to set something from DB to local storage using javascript.

So to show a table of users I am doing the following:

<table cellspacing="0">
      <tr>
         <th>ID</th>
         <th>Name</th>
         <th>View</th>
         <th>Edit</th>
      </tr>

        <?php
        $count=1;
        $sel_query="Select * from user_details ORDER BY id desc;";
        $result = mysqli_query($con,$sel_query);
        while($row = mysqli_fetch_assoc($result)) { 

        ?>

      <tr>
         <td align="center"><?php echo $count; ?>
         <td align="center"><?php echo $row["docname"]; ?>
         <td align="center"><a href="view.php?id=<?php echo $row["id"]; ?>">View</a></td>
         <td align="center"><a href="edit.php?id=<?php echo $row["id"]; ?>" onclick="handleLinkClick(event);">Edit</a></td>
      </tr>

<?php $count++; } ?>

   </table>

The above shows the records in the table perfectly.

Now when edit is clicked, I call a javascript function. (This is called before I click on edit? I am not sure why?)

<script type="text/javascript">

    function handleLinkClick (e) {
        e.preventDefault (); 
        var id = e.target.href.split ("?").pop ();
        var key = <?php echo add(id);?>

        console.log(key);
        localStorage.setItem ("myKey", key);
        //window.location.href = e.target.href;
        }

</script>

Now from javascript function, I am trying to call a PHP function a variable is sent from javascript to PHP function to fetch corresponding records details and return that data to javascript, below is the PHP function:

function add($id){

    $sel_query="select * from user_details where id=$id";
    $result = mysqli_query($con,$sel_query);
    while($row = mysqli_fetch_assoc($result)) 
    { 
        $key = $row["rawText"];
        echo $key;
    }

     return $key;
}
?>

I don't see any results. I am not sure why I am not getting any results? Can some give me the right syntax or correct me?

Thanks!

Sanjana Nair
  • 2,663
  • 6
  • 26
  • 44
  • I suggest you look at calling your `add` function by making an ajax call: https://stackoverflow.com/questions/15757750/how-can-i-call-php-functions-by-javascript – Nick Parsons May 12 '19 at 12:41
  • @NickParsons: How do I cal ajax function onClick? – Sanjana Nair May 12 '19 at 13:58
  • I'd set a Jquery on click listener and then inside of it make the Ajax call. If you need more information, refer to this post https://stackoverflow.com/questions/27716499/how-to-call-a-php-script-function-on-a-html-button-click – Matthew Vanlandingham May 12 '19 at 17:01

0 Answers0