Jump to content
Larry Ullman's Book Forums

Recommended Posts

How would one pass a php variable to a jQuery function.

 

I would like to use jQuery to only show the delete and edit links if $_POST['user_id'] =1.

 

 

// Table header:
echo '<table align="center" cellspacing="0" cellpadding="5" width="75%">
<tr>
<td align="left"><b>Edit</b></td>
<td align="left"><b>Delete</b></td>
<td align="left"><b><a href="view_users.php?sort=ln">Last Name</a></b></td>
<td align="left"><b><a href="view_users.php?sort=fn">First Name</a></b></td>
<td align="left"><b><a href="view_users.php?sort=rd">Date Registered</a></b></td>
</tr>
';
 
// Fetch and print all the records....
$bg = '#eeeeee'; 
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
echo '<tr bgcolor="' . $bg . '">
<td align="left"><a href="edit_user.php?id=' . $row['user_id'] . '">Edit</a></td>
<td align="left"><a href="delete_user.php?id=' . $row['user_id'] . '">Delete</a></td>
<td align="left">' . $row['last_name'] . '</td>
<td align="left">' . $row['first_name'] . '</td>
<td align="left">' . $row['dr'] . '</td>
</tr>
';
} // End of WHILE loop.
 
echo '</table>';

 

Link to comment
Share on other sites

Here's an example of what Larry's saying:

 

<?php
  
  $php_var = 5;
  
?><!DOCTYPE html>
 
<html lang="en">
  
  <head>
    
    <title>PHP variable to JavaScript/jQuery</title>
    
    <meta charset="UTF-8">
    
  </head>
  
  <body>
    
    <!-- Page structure here -->
    
    <script>
      
      var jsVar = <?php echo $php_var; ?>;
      
      alert(jsVar); // Will alert 5. Note that a semicolon is required in both the PHP and the JS.
      
    </script>
    
  </body>
  
</html>
Link to comment
Share on other sites

 Share

×
×
  • Create New...