Adding Javascript with php -
i typed code php let me if click 'delete' delete user database
i need put javascript code asks me when click on delete
'are sure !? '
<?php if(!isset($_get['id'])) { die('bad access'); } $_id = (int)$_get['id']; // avoid injection , typing codes in url if ($_id == 0) { die('bad access'); } require_once('usersapi.php'); $user = tinyf_users_get_by_id($_id); if($user == null){ tinyf_db_close(); die('wrong user id'); } $result = tinyf_users_delete($_id); tinyf_db_close(); if($result){ die('success'); } else{ die('falied'); } ?>
the file
showusers.php
<html> <body> <?php require_once('usersapi.php'); error_reporting(e_all); ini_set('display_errors', 1); /* function tinyf_users_get($extra ='') { global $tf_handle; $query = sprintf("select * `users` %s",$extra ); $qresult = mysqli_query($tf_handle, $query); if (!$qresult) return null; $recount = mysqli_num_rows($qresult); if ($recount == 0) return null ; $users = array(); for($i = 0 ; $i < $recount ; $i++) $users[count($users)] = mysqli_fetch_object($qresult); returns current row of result set //mysql_free_result($qresult); return $users; } */ $users = tinyf_users_get(); if($users == null) { die('problem'); } $ucount = count($users); if($ucount == 0) { die('no users'); } ?> <ul type = "square"> <?php for($i = 0 ; $i < $ucount ; $i++) { $user = $users[$i]; echo "<li><a href = \"userprofile.php?id=$user->id\">$user->name <a/> -- $user->email -- <a href = \"deleteuser.php?id=$user->id\"> delete <a/> | <a href = \"modifyuser.php?id=$user->id\"> edit <a/> </li>"; //$array -> //-- <a href = \"deleteuser.php?id=$user->id\"> delete <a/> | <a href = \"modifyuser.php?id=$user->id\"> edit <a/> } ?> </ul> </body> </html>
$result = tinyf_users_delete($_id); function delete user need before doing asks me if i'm sure
if have form, next code work:
<html> <head> <script type="text/javascript"> function ask () { return confirm( "are sure?" ); // returns true or false. } </script> </head> <body> <form method="post" action="deletion.php" onsubmit="return ask()"> enter user id delete <input type="text" name="uid" /> <br/> <input type="submit" value="delete user" /> </form> </body> </html>
notice how < form > tag ends "onsubmit". form won't submitted if javascript function "ask()" returns false.
edited code object. every user displayed form, name visible, delete button @ right, , id hidden input, php id of user hidden field:
<?php $user = new stdclass(); $user->id = "123"; $user->name = "user name"; $user->email = "aaa@bbb.ccc"; echo "<ul>"; echo '<form method="post" action="deletion.php" onsubmit="return ask()">' . ' user ' . $user->name . ' <input type="text" name="uid" value="$user->id" hidden />' . ' <input type="submit" value="delete" />' . '</form>'; echo "</ul>"; ?>
Comments
Post a Comment