dtoaillwk759656786 2018-05-12 03:05
浏览 415

Php echo按钮onclick获取id

I hava a dynamic list, loading from database in my php page.This list also have a delete button. When i click this button i need to get id of this button and delete from database that line with php code.

  $dbh = new PDO("sqlite:database.sdb");
  $sql ="SELECT  *      FROM clip";

foreach ($dbh->query($sql) as $row)
    {
    print 'Id: '. $row['id'] .'<br />';
    echo '<input type="submit" id="'.$row['id'].'" value="delete" name="del">';
    }

also this is my final code for button click;

$dbh->exec("delete clip where id='in here should be button id'");

How can i connect with this two code. Thanks already.

  • 写回答

2条回答 默认 最新

  • dps57553 2018-05-12 03:16
    关注

    You can use this working jquery template for your task. It binds to all inputs that has a name="del".

    <script src="http://code.jquery.com/jquery-latest.min.js"
            type="text/javascript"></script>
    
    <script>
    $(document).ready(function() {
      $('input[name="del"]').on('click', function() {
        alert('button id is '+$(this).attr('id'));
        var button_id = $(this).attr('id');
        $.ajax({
          method: 'post',
          url: "delete.php",
          data: {'did':button_id},
          dataType: "html",
          success: function(result) {
            alert(result);
          }
        });
      });
    });
    </script>
    

    In your PHP delete file, you can retrieve the delete id via $_POST['did'].

    评论

报告相同问题?