kwandoa Posted April 19, 2013 Share Posted April 19, 2013 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 More sharing options...
Larry Posted April 19, 2013 Share Posted April 19, 2013 You'd use PHP to write the value of the variable into the JavaScript/ Link to comment Share on other sites More sharing options...
HartleySan Posted April 19, 2013 Share Posted April 19, 2013 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 More sharing options...
kwandoa Posted April 21, 2013 Author Share Posted April 21, 2013 Great!!! awsome, now i understand!! I would like to use this method to validate forms and to play around with what HTML elements to load, depending on the user. Link to comment Share on other sites More sharing options...
HartleySan Posted April 21, 2013 Share Posted April 21, 2013 I'm not sure if you need to use the technique I outlined above to do either, but okay, glad you got what you asked for. Link to comment Share on other sites More sharing options...
Recommended Posts