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?

3
  • 3
    Never do operations like DELETE via the query line. Always go for POST. Commented Mar 29, 2013 at 10:56
  • 1
    It would take even a script kiddy no more than five seconds to delete your entire database with that setup. Commented Mar 29, 2013 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; stackoverflow.com/questions/1924939/php-request-vs-get-and-post Commented Mar 29, 2013 at 11:06

2 Answers 2

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  
    }

  });

 });
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't one need to put ' or " when passing string as data?
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.

12 Comments

What is the advantage of POST over GET?
What should I place on delete button where currently I placed this: onClick='window.location='page.php?action=del&id=1'
@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?
@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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.