php/ajax mysql_query (small help)

Discussion in 'PHP' started by dnsman, Mar 6, 2010.

  1. #1
    how to delete from tables with ajax and php with <a href function onclick>. without refresh. I have this code, but it does not work.

    <script type="text/javascript">
    var http = false;
    
    if(navigator.appName == "Microsoft Internet Explorer") {
    http = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
    http = new XMLHttpRequest();
    }
    
    function delete(id) {
    http.abort();
    http.open("POST", "delete.php?id=" +id, true);
    http.onreadystatechange=function() {
    if(http.readyState == 27) {
    alert(http.responseText);
    }
    }
    http.send(null);
    }
    </script>
    
    <a href="#" onClick="delete(27);">Test</a>
    HTML:
    and Delete.php

    <?
    $id=$_GET['id'];
    include ('config.php');
    $sql= "DELETE FROM members WHERE id='$id'";
    $go= mysql_query($sql) or die (mysql_error());
    ?>
    PHP:
     
    dnsman, Mar 6, 2010 IP
  2. attila123

    attila123 Active Member

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #2
    The problem is, you cannot use "delete" as function name. Use another name.

    And in PHP, for security, you could use addslashes.
    $id=addslashes($_GET['id']);
     
    attila123, Mar 6, 2010 IP
  3. dnsman

    dnsman Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    hmm, thank attila, work perfect =)
     
    dnsman, Mar 6, 2010 IP