0

Hi I'm trying to send an id using AJAX POST from my front-end to my AJAX php backend. However, I kept getting this error message "Undefined array key "data" " at my backend file...Im not too sure what I did wrong as I had tried many solutions from previous discussions with similar issue but still couldn't get it to work.

My code: front-end

<script>
     $(document).ready(function() {
                $(document).on('click', ".btn-edit", function() {
                  var payroll_history_id = $(this).data('id');
                  var _data = {
                    'id': payroll_history_id,
                  };
                  console.log("data for ajax call", _data);
                  console.log(payroll_history_id);
                  $.ajax({
                    url: "payroll-summary-data.php",
                    method: "POST",
                    dataType: "html",
                    data: {
                      _data
                    },
                    success: function(data) {
                      alert("Hello World");
                      alert(payroll_history_id);
                      alert(data);
                      // $("#payroll_history_id").val(data.payroll_history_id);
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                      alert(xhr.status);
                      alert(thrownError);
                    }
                  })
                })
              })
<script>

Backend:

<?php
$payroll_history_id = $_GET['data'];
json_decode($payroll_history_id);
$userobject = new User();
$payroll_historyobject = new payroll_history();
// $payroll_history_id = escape(Input::get('payroll_history_id'));
$payrollresult = $payroll_historyobject->searchOnly2($payroll_history_id);
$payroll_history_user_id = array();
echo $payroll_history_id;
print_r($payrollresult);
$payroll_historyresultOnly = $payroll_historyobject->searchOnlypayroll_history($payroll_history_id);

for ($i = 0; $i < count($payrollresult); $i++) {
    array_push($payroll_history_user_id, $payrollresult[$i]->user_id);
}
?>
  • `method: "POST"` vs `$_GET['data']` – brombeer Feb 16 '22 at 11:37
  • You are sending a _POST_ request, yet you are trying to access `$_GET['data']`. The URL `payroll-summary-data.php` did not contain any query string parameter named `data`, so of course `$_GET['data']` does not exist. – CBroe Feb 16 '22 at 11:38
  • @CBroe hi, Sorry about this, Im kinda new into these web development stuff, what should I do ? – Jenkins2000 Feb 16 '22 at 11:43

1 Answers1

-1

you are sending data by POST method and getting by GET method so,

replace your code in Back-end:

$payroll_history_id = $_GET['data'];

to

$payroll_history_id = $_POST['data'];

i think it will work perfectly.

  • Hi, thx for providing me solution, unfortunately it still shows the same error Undefined array key "data" after I changed it from get to post. Any ideas why it kept showing that error ? – Jenkins2000 Feb 16 '22 at 11:54