Little help in ajax get method

Discussion in 'JavaScript' started by minhazikram, May 21, 2012.

  1. #1
    i have a database with columns id and name. I have inserted the value manually and then retrieving the values using AJAX function.
    Below I am attaching the function using which i am retrieving data from the database.

    <script type="text/javascript">
    window.onload = showALL;
    function showALL()
    {
    var xmlrqst;
    if(window.XMLHttpRequest){
    xmlrqst = new XMLHttpRequest();
    }
    else{
    xmlrqst = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlrqst.onreadystatechange = function(){
    if(xmlrqst.readyState==4 && xmlrqst.status==200)
    {
    document.getElementById("userall").innerHTML = xmlrqst.responseText;
    }
    }
    xmlrqst.open("GET","post.php?get=1",true);
    xmlrqst.send();
    }// showAll()
    </script>

    the aforementioned javascript is in the index.php.
    here you can see that i am using a post.php and is passing parameter through the url to post.php

    below is the code that is being executed in the post.php

    <?php
    if(isset($_GET['get']))
    {
    if($_GET['get']!=1)
    {
    echo "error";
    }
    $showAll = "SELECT * FROM names";
    $showAllQuery = mysql_query($showAll,$con);
    if(!$showAllQuery){ die("error".mysql_error());}
    echo "<table>";
    echo "<th>ID</th>";
    echo "<th>NAME</th>";
    while($output=mysql_fetch_array($showAllQuery))
    {
    $out = $output['id'];
    echo"<tr>";
    echo "<td>{$output['id']}</td>";
    echo "<td>{$output['name']}</td>";
    echo "<td><a href=\"#\">delete</a></td>";
    echo "</tr>";

    }
    echo "</table>";



    }

    ?>

    in the post.php i am generating the table with user data and it is being displayed in the index.php file in the <div ></div> section.

    Now, lets discuss my problem. The problem is the data are generated in the post.php and is displayed in the index.php in a certain div.
    How can i delete value from database using AJAX.
     
    Solved! View solution.
    minhazikram, May 21, 2012 IP
  2. #2
    Create a function in your JavaScript file called "deleteID" for example where it sends an id to your "post.php" file
    An example of that would include this line in your function: (the part that is in red - "id" is passed as a parameter like deleteID('58'):
    [COLOR=#111111]xmlrqst.open( "GET[/COLOR][COLOR=#111111]", "post.php?delete="+[/COLOR][COLOR=#ff0000][B]id[/B][/COLOR][COLOR=#111111],true);[/COLOR]
    Code (markup):



    In your "post.php" code, the part that says:
    echo "<td><a href=\"#\">delete</a></td>";
    PHP:
    should be:
    echo "<td><a href=\"#\" onclick=\"deleteID('".$output['id']."');\">delete</a></td>";
    PHP:

    In your post.php, you would write code that gets the delete id from the url that is sent from your JavaScript's "deleteID" function, and deletes that record.
    if(isset($_GET['delete'])){
    $id_to_delete = $_GET['delete']
    //code here to delete the record with that id in the database
    }
    
    PHP:

    This is just an example, of course there's more to it. So good luck.
     
    HostPlanz, May 21, 2012 IP
  3. minhazikram

    minhazikram Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Thanks. This is really helpful.
     
    minhazikram, May 22, 2012 IP