0

html:

<form class="form-horizontal" role="form" autocomplete="off" action="" method="POST" name="formular_comanda" id="formular_comanda">

<input type="number" class="form-control input-sm" name="inaltime_best" id="inaltime_best" placeholder="H (mm)" required="required" onchange="calculator_piese()" />

</form>

JS:

function calculator_piese() {
  var height = document.getElementById("inaltime_best");
  $.ajax({
    type: "POST",
    url: "calculator/panouri-simple/calcul-panouri-simple.php",
    data: $("#formular_comanda").serialize(),
    success: function(result) {
      document.getElementById("show_panels").innerHTML = result;
      console.log(height.value); // this return my input value
    },
    error: function(result) {
      console.log("Eroare:");
      console.log(result);
      console.log();
    }
  });
}

calcul-panouri-simple.php

error_reporting(E_ALL);
$height = $_POST['height'];
var_dump($height); //this returns NULL

I have a problem. My php returns error:

Notice: Undefined index: height in ... calcul-panouri-simple.php on line 3

console.log(height.value) returns me the value, but from php var_dump i get NULL.

I don't understand what is wrong.

Pedram
  • 15,766
  • 10
  • 44
  • 73
Bogdan C
  • 389
  • 1
  • 4
  • 17
  • It's because height is not part of your form - you can add it to the end like this: `$("#formular_comanda").serialize() + '&height=' + height` – Pete Nov 29 '17 at 12:28
  • But it is between form tags. What do you mean is not part of my form? – Bogdan C Nov 29 '17 at 12:28
  • It's not an input on your form - you just set it as a variable from the input called `inaltime_best`, if you do not want to do the above, then just use `$_POST['inaltime_best']` – Pete Nov 29 '17 at 12:29
  • `var height` not `POST` via `ajax` because you wrote it before `ajax call` – Pedram Nov 29 '17 at 12:29
  • have you tried using `FormData`? https://stackoverflow.com/a/27774479/1732775 – roomcayz Nov 29 '17 at 12:46
  • Are you sure that's the problem? The input name is `inaltime_best`, while you're trying to get a value from `height` :) – roomcayz Nov 29 '17 at 12:50
  • @Roomy Indeed you are right. – Bogdan C Nov 29 '17 at 12:59
  • @Pete you are also right. I thought the index from POST should be the one I mention in js, like var height = document... but it turns out that POST value takes value directly from form through serialize(). – Bogdan C Nov 29 '17 at 13:00

1 Answers1

0
Because height field is not in your form so you can be use 

function calculator_piese() {
  var height = document.getElementById("inaltime_best");
  $.ajax({
    type: "POST",
    url: "calculator/panouri-simple/calcul-panouri-simple.php",
    data: $("#formular_comanda").serialize() + '&height=' + height,
    success: function(result) {
      document.getElementById("show_panels").innerHTML = result;
      console.log(height.value); // this return my input value
    },
    error: function(result) {
      console.log("Eroare:");
      console.log(result);
      console.log();
    }
  });
}