-1

I'm adding a new record in my MySql DB with javascript and I have to elaborate this function in PHP.

$(document).on('submit', '#product_form', function(event){    
event.preventDefault();
  //btn_action="add_pricelvl"; //Set variable to call the add new item 
  var valdata = $(this).serialize(); //Array with field value 
  var tax = $('#item_tax').val(); //checkbox tax 
  var taxvalue = $('#item_taxvalue').val(); //inputbox tax
  var tabledets = it_det //Read the detail table
   .rows()
   .data();
  var arr1=[];//Declare the array 
  var i=0;
  //Put the datatable(item_details) rows in the array
  for (i=0; i<tabledets.length; i++){
   arr1[i]=tabledets.rows(i).data(); 
  }
  //call ajax function and send variable to php file.
  $.ajax({
   processData: false,
   url:'item_action.php',
            method:"POST",
            data:{
    //btn_action:btn_action, 
    valdata:valdata,
    tax:tax,
    taxvalue:taxvalue,
    arr1:arr1
    },   
            success : function(data)
            {
            ....
            }
            error : function () {
        ....
        }
      })
    });
<?php
if ($_POST['btn_action']=='add_pricelvl'){
  $query="....."
  //Getting variables by JavaScript
}
?>

I can't get any variable in my PHP file... how is that possible? Trying to check any $_POST[variable] in the PHP File they are NULL... why?

davidkonrad
  • 83,997
  • 17
  • 205
  • 265
Fabio
  • 85
  • 1
  • 2
  • 14

2 Answers2

0

Try removing processData: false,. That command is telling jQuery to not turn the object into a query string. If you want to use $_POST[] in your script, it will expect it to be sent over as a query string.

http://api.jquery.com/jQuery.ajax/

processData (default: true)
Type: Boolean
By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

Taplar
  • 24,788
  • 4
  • 22
  • 35
  • If I turn processData to true I will get an error "TypeError: 'insertCell' called on an object that does not implement interface HTMLTableRowElement.". That's why I turn off processData – Fabio May 18 '18 at 17:36
  • Then I would probably suggest changing the question, or opening another, asking out *that* error. As it stands, you are asking about an error with something you are trying to do to fix another error, so this is pretty much an X/Y problem. https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem @Fabio – Taplar May 18 '18 at 17:38
  • And that's what I did before... https://stackoverflow.com/questions/50398130/error-saving-data-from-datatable-to-mysql-database ... Honestly I was sure I fixed my problem turning off processData – Fabio May 18 '18 at 17:41
-1

To get the body from a POST call you should use something like this

$json = file_get_contents('php://input');
$json = urldecode($json);
$obj = json_decode($json, true);

Then you can manipulate $obj variable as a regular php array.