0

I'm new to php.
I want to use jquery to delete a table row from php echoing code, but it doesn't work. Here is my html code:

<!DOCTYPE html>
<html>
<head>
<title>test insert</title>
<script src="jquery-2.0.3.js"></script> 
<script type="text/javascript">
$(document).ready(function(){
    $('.del').click(function(){
        $(this).parent().parent().fadeOut('slow');
    });

    $('#show').click(function(){
        $.post('data.php',{action: "show"},function(res){       
            $('#result').hide().html(res).fadeIn('slow');   
        });     
    });
});
</script>
</head>
<body>
   <h2>Show Data</h2>
   <button id="show">Show</button>
   <p>Result:</p>
   <div id="result"></div><br>
</body>
</html>

and here is my php code:

<?php
//connect to db
$con = mysql_connect('localhost','root');
$db = mysql_select_db('test');
//if show key is pressed show records
if($_POST['action'] == 'show'){
    $sql   = "select * from customer order by Firstname";
    $query = mysql_query($sql);

    echo "<table><tr><th>Firstname</th><th>Lastname</th><th>Keynumber</th>
    <th>Number2</th><th>Number3</th><th>Option</th><th><button class='del' >delete</button></th></tr>";
    while($row = mysql_fetch_array($query)){
        echo "<tr><td>$row[firstname]</td><td>$row[lastname]</td><td class='key'>$row[keynumber]</td>
        <td>$row[number2]</td><td>$row[number3]</td><td><button class='del' >delete</button></td></tr>";
    }
    echo "</table>";
}
?>

When I press the 'delete' button, it doesn't work.
I don't know why it doesn't work:(

0

2 Answers 2

2

It looks like your data is loaded dynamically when the show button is clicked. If so, you need to use .on() instead of .click(), and .on()'s delegated event syntax. Change your delete button code to:

$(document).on('click', '.del', function(){
    $(this).parent().parent().fadeOut('slow');
});
Sign up to request clarification or add additional context in comments.

1 Comment

+1, although going up to $('#result') may be enough in this case.
0

I have corrected ur code just check it,

<script type="text/javascript">
$(document).ready(function(){
    $('.del').click(function(){
        $(this).parent().parent().fadeOut('slow');
    });

    $('#show').click(function(){
        $.post(
         url:'data.php',
         data:{action: "show"},
         type:'POST',
         function(res){       
            $('#result').hide().html(res).fadeIn('slow');   
        });     
    });
});

</script>

Comments

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.