【问题标题】:php function call from on button click using jquery/ajax使用 jquery/ajax 从按钮单击调用 php 函数
【发布时间】:2014-05-21 23:22:19
【问题描述】:

试图在按钮单击时调用 php 函数以从我正在循环的 db 中删除行...需要将按钮标记 id 属性传递给函数。搞不定

$('button').click(
    function(){
        var id = $(this).attr('id');
        $.ajax({
            url: "ins.php",
            type: "POST",
            data: id,
            success: function(data){
            }
        });
    });

// html button in table to delete a row
<td><button id='.$i.' name="delete"><img src="delete.png"></button></td>

// ins.php
if (isset($_POST['delete'])){
    DB_delete($conn, $_POST['delete']); 
}

// function that deletes the row
function DB_delete($conn,$id){

    $sql = "DELETE FROM person WHERE id = ?";

    $stmt=$conn->prepare($sql);
    $stmt->bind_param('i',$id);
    $stmt->execute();
    $stmt->close();
}

【问题讨论】:

    标签: php jquery ajax function button


    【解决方案1】:

    您根本没有正确传递数据。由于您正在寻找 $_POST['delete'],因此您必须使用该键/值提交它。

    $.ajax({
          url: "ins.php",
          type: "POST",
          data: {"delete": id},
          success: function(data){
          }
      });
    

    【讨论】:

      【解决方案2】:

      您在 ajax 调用中的数据参数不正确。现在它只是一个普通数字,但它应该是一个对象,以便在您的 ins.php 中被视为 $_POST 或 $_GET(取决于 'type' 参数)。所以应该是这样的:

      $.ajax({
        url: "ins.php",
        type: "POST",
        data: {"delete": id},
        success: function(data){
        }
      });
      

      【讨论】:

      • 在 ins.php 中运行 var_dump($_POST['delete']) 会得到什么?
      猜你喜欢
      • 2017-04-21
      • 1970-01-01
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多