SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    deleting using javascript

    hi all,
    i want javascript function to delete the selected records for the given php program below....
    also i am not getting field values in correct order one field is more than its field...
    kindly tell me how to do it......
    Code:
    <?php
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password="";     // Mysql password
    $db_name="test"; // Database name
    $tbl_name="emp"; // Table name
    
    // Connect to server and select databse.
    mysql_connect($host, $username, $password)or die("cannot connect");
    mysql_select_db($db_name)or die("cannot select DB");
    $sql="SELECT * FROM emp";
    $result=mysql_query($sql);
    $count=mysql_num_rows($result);
    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td align="center" bgcolor="white"><strong>EmpNo</strong></td>
    <td align="center" bgcolor="white"><strong>EmpName</strong></td>
    <td align="center" bgcolor="white"><strong>Desig</strong></td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result, MYSQL_ASSOC))
    {
    
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['empno']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['empname']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['desig']; ?></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    </tr>
    <?php
    // Check if delete button active, start this
    if($_POST['delete'])
    {
    //print_r($_POST);
    //exit;
    for($i=0;$i<count($_POST['checkbox']);$i++)
    {
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
    $result = mysql_query($sql);
    }
    
    // if successful redirect to delete_multiple5.php
    if($result){
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=h_delete.php\">";
    }
    }
    mysql_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>

  2. #2
    SitePoint Enthusiast Anthony.Barnes's Avatar
    Join Date
    Aug 2011
    Location
    Canberra, Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have any javascript that you have written already? I see nothing in your example. You have alot of issues in there you need to sort out besides adding javascript on top:

    • Escaping SQL arguments to protect against SQL injection
    • Add IDs to your table and form element
    • Deprecated HTML attributes (use CSS?)
    • Horrible method to redirect


    Easiest way to add javascript to this, download jQuery and:

    Code JavaScript:
    $(document).ready(function(){
    var $form = $('form[name="form1"]'); // This would be easier with a form id
    $('#delete').click(function(e){
        e.preventDefault(); // Prevent default action of the submit button
        $.ajax({
             type: 'POST',
             data: $form.serialize(),
             url: $form.attr('action'), // There is no action currently set?
             success: function() {
                  // Finished
             }
        });
    });
    });

    Definitely sort out the other issues with the PHP before anything though.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here i have written to delete using javascript......
    it is "del.php"
    Code:
    <script>
    function fundel(sno)
    {
    rv=confirm("u want to delete");
    if(rv==true)
    {
    location="delete.php?seno="+sno;
    }
    }
    </script>
    <table border="2">
    <?php
    mysql_connect("localhost","root","");
    mysql_select_db("test");
    $data=mysql_query("select * from emp");
    while($rec=mysql_fetch_row($data))
    {
    echo "<tr><td>$rec[0]<td>$rec[1]<td>$rec[2]</tr>
    <input type="button" value="delete" onclick='fundel($rec[2])'>";
    }
    ?>
    also is the "delete.php"
    Code:
    <?php
    $qs=$_REQUEST['seno'];
    mysql_connect("localhost","root","");
    mysql_select_db("test");
    mysql_query("delete from emp where sno=$qs");
    header("location:getrec.php");
    ?>
    it is displaying the error as Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\del.php on line 19

    tell me what went wrong....
    whether it will delete the values.....

  4. #4
    SitePoint Enthusiast Anthony.Barnes's Avatar
    Join Date
    Aug 2011
    Location
    Canberra, Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is a PHP issue you should visit PHP section in these forums. There are so many things wrong with that code that you posted I'm not sure where to start, I'm sure the good folks in the PHP area can point you in the right direction.

    I'd also try running these terms through a search engine to learn a bit more about "SQL Injection", "HTML standards" and "Cross site scripting".


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •