0

I want to delete value from database using PHP.

Using a table row and placed a button like "Delete" and I create a function like onClick='window.location='page.php?action=del&id=1' and in PHP for delete.

if($_REQUEST['action'] == 'del'){
     $req_id = $_REQUEST['id'];
     $del_query = mysql_query("DELETE FROM table WHERE id='$req_id'");
}

It's working well but, I don't want to refresh the page. Please tell me how can I do it without page refresh?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JoJo
  • 1,443
  • 2
  • 12
  • 12
  • 3
    *Never* do operations like DELETE via the query line. *Always* go for *POST*. – techfoobar Mar 29 '13 at 10:56
  • 1
    It would take even a script kiddy no more than five seconds to delete your entire database with that setup. – adeneo Mar 29 '13 at 11:00
  • Apart from SQL injection, using `$_REQUEST` is *bad* as well, `$_REQUEST` contains information from **cookie**, **post** and **get**. It's best to be specific: use `$_POST` or `$_GET`, depending on what you're expecting; http://stackoverflow.com/questions/1924939/php-request-vs-get-and-post – thaJeztah Mar 29 '13 at 11:06

2 Answers2

1
<button id='delete'>Click Me To Delete</button>

 $('#delete').on('click',function(){

 $.ajax({
    type:'POST',
    url:'page.php',
    data:'action=del&id=1',
    success:function(result){
              //Deleted  
    }

  });

 });
XTop
  • 255
  • 2
  • 9
0

Try this:

<a href="javascript:void(0);" onclick="if('Are you sure you want to delete?'){delData(this,1);}">delete</a>

<script type="text/javascript">
function delData(control,id){
  $.ajax({type:'POST', url:'page.php?action=del&id='+id, success:function(result){
   //alert('deleted');
     $(control).parents('table').eq(0).remove();
   }
  });
}
<script>

NOTE:

  1. You will need to add jQuery file for this.
  2. This process is called ajax. Google it.
  3. Then try to pass the data in post parameter rather than in query string.

Update: Note that now the 'this' is passed as a reference of this control to the method delData.

Gaurav Pandey
  • 2,798
  • 4
  • 28
  • 41
  • What is the advantage of POST over GET? – jantimon Mar 29 '13 at 11:02
  • What should I place on delete button where currently I placed this: onClick='window.location='page.php?action=del&id=1' – JoJo Mar 29 '13 at 11:05
  • @jantimon If you ask what is the advantage of POST over GET HERE? Then that's a valid question, I just wanted to mention an approach so that at least in non-ajax requests, it wont be showing in the address bar itself. In ajax how can to send json data as a request parameter by the way? – Gaurav Pandey Mar 29 '13 at 11:06
  • @user2140704 See the updated answer, it is just for the reference as I am passing hard coded 1 as id. You can modify it according to your needs. – Gaurav Pandey Mar 29 '13 at 11:09
  • 1
    Search-engine indexing machines use 'GET' to crawl your website. Imagine Google indexing your website and following these links; `page.php?action=del&id=1`, `page.php?action=del&id=2`. `POST` is for *sending (hence 'post') data* and should be used to modify data. `GET` is to *retrieve (hence 'get') data*. – thaJeztah Mar 29 '13 at 11:10
  • Its working for me but when I click on delete its showing a dialogue box called deleted and then I press ok on that dialogue value still there and when i refresh my page then its removed.. I want to remove and hide it immediately on clicking delete button.. – JoJo Mar 29 '13 at 11:41
  • Yeah.. you can see an `alert('deleted');` written inside success function. That is the place where you need to do something. Edit your question and post full relevant code, or do it your self. You just have to remove the related div. A better approach would be to return partial view from server if you are using php but anyways. Something like `$(control).parent('div').remove();` should remove it. See the updated answer. – Gaurav Pandey Mar 29 '13 at 11:44
  • I use $(control).parent('table').remove(); but its not working still same when I refresh then it is going to hide.. :( also remember i want to use if(confirm('Do you really want to remove it ?')) before deleting it.. Thanks – JoJo Mar 29 '13 at 11:53
  • Here is full code: `` AND ` function delData(id){ $.ajax({type:'POST', url:'manage_reports.php?action=del&id='+id, success:function(result){alert('deleted');}}); $(control).parent('table').remove(); }` – JoJo Mar 29 '13 at 11:55
  • See the updated code. You have to use `$(control).parents('table').eq(0).remove();` and also notice the implemented confirm box there. – Gaurav Pandey Mar 29 '13 at 12:07
  • I replaced the code with this: $(control).parents('table').eq(0).remove(); but still not working.. – JoJo Mar 29 '13 at 14:39
  • ok.. then google a bit about how to remove any html element using jquery as no one can assume what is there in your html markup so can't exactly tell you what to remove or what to write – Gaurav Pandey Mar 29 '13 at 15:32