views:

1085

answers:

3

hi all friends

I am using php / mysql and protype.js to delete record from a table. The problem is that the record in the database is not deleted.

index.php:

       <a href="javascript: deleteId('<?php echo $studentVo->id?>')">Delete</a></td>

Script is

   function deleteId(id)
   {
       alert("ID : "+id);
       new Ajax.Request('delete.php?action=Delete&id='+id,{method:'post'});
       $(id).remove(); // because <tr id='".$row[id]."'> :)

   }

delete.php

     <?php
      /* Database connection */
      include('configuration.php');
      echo "hello,...";
      if(isset($_POST['id'])){
          $ID = $_POST['id'];
          $sql = 'DELETE FROM student where id="'.$ID.'"';
          mysql_query($sql);
      } 
      else { echo '0'; }
     ?>

alert("ID : "+id); is working properly but the code after that is not.

+8  A: 

You are using a GET request, from JS :

{method:'get'}

And your PHP code uses data he thinks arrives as POST :

$ID = $_POST['id'];

You should use the same method on both sides.

(As you are modifying / deleting data, you should probably use POST)


As a sidenote, you should definitly escape/protected/check the data you are using in the SQL query, to avoid SQL injections, using, for instance, intval as you are working with an integer ; you'd use mysql_real_escape_string if you were working with a string.

Another way would be to stop using the old mysql extension, and start using mysli or PDO, which means you could use prepared statements (mysqli, pdo)


EDIT after the comment : you also, now that the request is made in POST, need to change the way parameters are passed : they should not be passed in the URL anymore.

I suppose that something like this should work :

var myAjax = new Ajax.Request(
  'delete.php',
  {
    method: 'post',
    parameters: {action: id}
  });

Or you could also use something like this, building the parameters string yourself :

var myAjax = new Ajax.Request(
  'delete.php',
  {
    method: 'post',
    parameters: 'action=' + id
  });

(Not tested, so you might have to change a few things ;-) )

For more informations, take a look at Ajax.Request and Ajax options :-)

Pascal MARTIN
ok dear i have done {method:'post'} and php file $ID = $_POST['id'];but there are not working
Ashvin Ranpariya
You also need to change the way the parameter is passed : if you are now using POST, the parameter should not be passed in the URL, but using the 'parameters' option ; see http://www.prototypejs.org/api/ajax/request and http://www.prototypejs.org/api/ajax/options for more informations
Pascal MARTIN
Ok now record can be delete but I need to refresh the page, to see changes. So how can I code for remove that record from current page? Server response would be bool so that we can determine that record is deleted or not. Can you help me to develop this??
Ashvin Ranpariya
Hi, this is quite a different question (you might want to actually ask another question) ; you have at least two possibilities : either remove something (the line corresponding to one student) from the DOM, in pure JS, after the Ajax call is finished ; or modify your PHP script so it returns a bunch of HTML that you will use to update some part (like the list of students) of the page at the end of the Ajax call -- which one you choose depends at least on the complexity of your page ; for instance, you might want to take into consideration things like pagination...
Pascal MARTIN
A: 

It seems like your ajax request is using GET but your delete script is trying to retrieve the id via POST. Try setting {method:'get'} to {method:'post'}.

theIV
ok dear i have done {method:'post'} to {method:'post'}.but there is no result
Ashvin Ranpariya
A: 

Change delete.php like this:

<?php
      /* Database connection */
      include('configuration.php');
      echo "hello,...";
      if(isset($_GET['id'])){
          $ID = $_GET['id'];
          $sql = 'DELETE FROM student where id="'.$ID.'"';
          mysql_query($sql);
      } 
      else { echo '0'; }
     ?>

Or alternatively you cold use $_REQUEST instead of $_GET (or $_POST in the first example) as it aggregates $_GET and $_POST arrays.

Gergely Orosz
Ok brother ,now record can be delete but I need to refresh the page, to see changes. So how can I code for remove that record from current page? Server response would be bool so that we can determine that record is deleted or not. Can you help me to develop this??
Ashvin Ranpariya