1

I'm sending an ajax "POST" in my php file. But the problem is the index of the POST is undefined.

This is my sample code in ajax.

$(".add-percentage").click(function(){

    var percentage_id = $(this).data('landing_id-percentage');

    $.ajax({
        url: 'ajax/readPercentage.php',
        type: 'POST',
        data: { percentage_id : percentage_id },
        success: function(data) {
          alert(data);
        },
        error: function(request, status, error){
          alert("Error!! "+error);
        }             
  });
});

My php code having an undefined index POST..

if(isset($_POST['percentage_id'])){

   $percentage_id = $_POST['percentage_id'];

   $query = mysqli_query($conn, "SELECT * FROM percentage WHERE percentage.percentage_id = '$percentage_id'");
}else{
  echo "Index is not properly set!";
}

I hope someone can help me. Thanks in advance.

2 Answers2

0
if($_SERVER['REQUEST_METHOD']=="POST"){
   $data = file_get_contents('php://input');
   print_r($data); // for testing purpose.

   /*
   $query = mysqli_query($conn, "SELECT * FROM percentage WHERE percentage.percentage_id = $data[0]['pecentage_id']");
   */
}else{
  echo "Index is not properly set!";
}
Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35
  • Bro when the success: function(data) { alert(data); }, the values are displayed but in the php file is not being displayed is returns else statement. – Unus Instar Omnium Oct 22 '18 at 04:04
  • yes bro I tried the code above it will return else statement. – Unus Instar Omnium Oct 22 '18 at 04:10
  • @UnusInstarOmnium you have an issue in your AJAX request. Your API expects for POST request, `if($_SERVER['REQUEST_METHOD']=="POST")`. Use `Postman` to check your API, https://www.getpostman.com/. – Salim Ibrohimi Oct 22 '18 at 04:27
  • bro @Salim Ibrogimov, when I alert the data from php response the output data is Array([landing_id]=>7) but when in displaying the print_r($_POST); from the php the output data is Array( ). – Unus Instar Omnium Oct 22 '18 at 10:02
  • bro @Salim Ibrogimov, when I reload the whole page the readPercentage.php is already undefined the index percentage_id. Then when I submit the data from ajax the php POST did not received the data and it display Array( ). What is the possible problem of this? – Unus Instar Omnium Oct 22 '18 at 10:27
  • @UnusInstarOmnium I told you, you can't retrieve POST request's data via `$_POST`. You should try this: `$data = file_get_contents('php://input'); print_r($data);`. – Salim Ibrohimi Oct 23 '18 at 03:13
0

Please use this code for ajax

$(".add-percentage").click(function(){

    var percentage_id = $(this).data('landing_id-percentage');

    $.ajax({
        url: 'ajax/readPercentage.php',
        type: 'POST',
        dataType: 'json',
        data: { percentage_id : percentage_id },
        success: function(data) {
          alert(data.status);
        },
        error: function(request, status, error){
          alert("Error!! "+error);
        }             
  });
});


<?php
$result_array= array();
if(isset($_POST['percentage_id'])){

   $percentage_id = $_POST['percentage_id'];
   $select_query = "SELECT * FROM percentage WHERE percentage.percentage_id =".$percentage_id;

   $query = mysqli_query($conn,$select_query);
   $result_array['status'] = 'success';
   $result_array['success_msg'] = 'Data get successfully';
}else{
  //echo "Index is not properly set!";
  $result_array['status'] = 'failure';
  $result_array['error_msg'] = 'Index is not properly set!';
}
echo json_encode($result_array);
    die();
?>
Maulik patel
  • 2,546
  • 2
  • 20
  • 26