danjesama Posted March 16, 2017 Share Posted March 16, 2017 Hi guys, I have a table with a delete column with deletes individual rows from the column, I am looking to add a delete warning. Now I know you can add a javascript function but im unsure how to do this as my page is built in PHP. Here is the delete column: echo '<td data-sort-initial="Descending"><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; Here is my delete.php file: <?php /* DELETE.PHP Deletes a specific entry from the 'players' table */ // connect to the database include('connect-db.php'); // check if the 'id' variable is set in URL, and check that it is valid if (isset($_GET['id']) && is_numeric($_GET['id'])) { // get id value $id = $_GET['id']; // delete the entry $result = mysql_query("DELETE FROM players WHERE id=$id") or die(mysql_error()); // redirect back to the view page header("Location: view.php"); } else // if id isn't set, or isn't valid, redirect back to view page { header("Location: view.php"); } ?> Thanks in advance Link to comment Share on other sites More sharing options...
davej Posted March 16, 2017 Share Posted March 16, 2017 It's not exactly clear what you are asking but you could use some javascript with a confirm popup. The javascript would set values in a URL or hidden form and then submit it. https://www.w3schools.com/jsref/met_win_confirm.asp Link to comment Share on other sites More sharing options...
john_jack Posted March 16, 2017 Share Posted March 16, 2017 (edited) If you want to use Javascript, here's the aproach : replace the following : 10 hours ago, danjesama said: echo '<td data-sort-initial="Descending"><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>'; with this : echo '<td data-sort-initial="Descending"><a href="#" onclick="delete_row(' . $row['id'] . ')">Delete</a></td>'; now we will define the function delete_row(x) as a javascript function : function delete_row(x) { // this is to avoid firing the default click on the <a> element event.preventDefault(); var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("errordiv").innerHTML = this.responseText; } }; xhttp.open("GET", "delete.php?id=" + x , true); xhttp.send(); } you will have to add a <div id="errordiv"></div> in the page that displays the Rows . all you have to do now is echo the errors on the delete.php like this : <?php /* DELETE.PHP Deletes a specific entry from the 'players' table */ // connect to the database include('connect-db.php'); // check if the 'id' variable is set in URL, and check that it is valid if (isset($_GET['id']) && is_numeric($_GET['id'])) { // get id value $id = $_GET['id']; // delete the entry $result = mysql_query("DELETE FROM players WHERE id=$id") or die(mysql_error()); // redirect back to the view page echo 'the player id '.$id.' was succesfully deleted'; //header("Location: view.php"); } else // if id isn't set, or isn't valid, redirect back to view page { echo 'The id provided was empty or not Numeric '; //header("Location: view.php"); } ?> so no more redirections . keep in mind that the mysql_ functions are outdated i suggest you use PDO or Mysqli to acces your database .checkout this for more info about Ajax https://www.w3schools.com/xml/ajax_intro.asp Goodluck Edited March 16, 2017 by john_jack i forgot to mention a link Link to comment Share on other sites More sharing options...
davej Posted March 16, 2017 Share Posted March 16, 2017 Yes, as John-Jack suggests you can learn to use AJAX, or you can redirect to a URL from Javascript similar to what you are doing now. It depends on whether you want to reload the page or not. I tend to use hidden forms for this sort of thing, but your GET approach is perfectly fine. See... https://www.w3schools.com/jsref/met_loc_assign.asp Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now