即使正确传递数据,为什么仍会有“通知:未定义的索引:操作”?

时间:2019-04-08 00:54:35

标签: php ajax

我仍然得到这个

  

注意:未定义索引:操作

即使正在传递数据并且所有功能都在工作。 所有这些AJAX调用所做的只是发布到一个php文件中,该文件将删除数据库表中的一行。

在php文件的第17行获得通知

$action = $_POST['action'];

有什么想法,即使它读取并且所有功能都起作用,为什么仍未定义?

我应该将通知静音吗?

---------------------------------- JS ------------ ------------------------------

function deleteOffer(id){// id is the button id which is the offerId on the fruit_offer table in the DB

  action = 'deleteOffer';

  $.ajax({
    url: 'deleteSubmission.php', //Post to File    
    type: 'POST', //Request Type    
    data: {'action' : action, 'offerId' : id} //Pass data along  
  });
window.location.href=window.location.href; // Reload page after complete
}
function deleteRequest(id){ // id is the button id which is the requestId on the fruit_request table in the DB

  action = 'deleteRequest';

  $.ajax({
    url: 'deleteSubmission.php', //Post to file
    type: 'POST', //Request Type
    data: {'action' : action, 'requestId' : id} //Pass data along
  });
window.location.href=window.location.href; // Reload page after complete
}

--------------------------------------- PHP ------- -----------------------------

<?php
require 'DBConnect.php'; //Connect to the DB

$action = $_POST['action'];

if($action == 'deleteOffer'){ //Check if action is deleteOffer from ajax

    $offerId = $_REQUEST['offerId'];

    $sql = $pdo->prepare( "DELETE FROM fruit_offer 
        WHERE  offerId = ?"); // Prepare the SQL Query 

    $sql -> bindParam(1, $offerId, PDO::PARAM_STR, 50); // Bind Parameter $offerId to STR(50) (more secure)

    $sql -> execute();

    $pdo = null; //Set $pdo & $sql to null (more secure)
    $sql = null;
    }// if

if($action == 'deleteRequest'){ //Check if action is deleteRequest from ajax

    $requestId = $_REQUEST['requestId'];

    $sql = $pdo->prepare( "DELETE FROM fruit_request 
        WHERE  requestId = ?"); //preparing sql statement to execute

    $sql -> bindParam(1, $requestId, PDO::PARAM_STR, 50); //Bind parameter $requestId to STR(50) (More Secure)

    $sql -> execute();

    $pdo = null; //Set $pdo & $sql to null (more secure)
    $sql = null;
    }//if

?>

0 个答案:

没有答案