-1

Have issue with PHP $_POST. Then I save information and check $_POST['g_kcal']; returned

Notice: Undefined index: g_kcal in

This field g_kcal count's all input fields values after input change. It returns new input with name = 'g_kcal' via ajax.

JavaScript:

$('.box-body').change(function(){
    var totalPointsKcal = 0;

    $(this).find('input[name="kcal[]"]').each(function(i,n){
        totalPointsKcal += parseFloat($(n).val(),10); 
    });

    $.ajax({
        type: 'post',
        url: 'ajaxLoader.php',
        data: {
            galutinis_kcal:totalPointsKcal,
        },
        success: function (response) {
            document.getElementById("galutinis").innerHTML=response;
        }
    });
});

ajaxLoader.php:

if(isset($_POST['galutinis_kcal'])) {
    $kcal = $_POST['galutinis_kcal'];

    echo "<input class='skaiciuokle kcal1' type='text' name='g_kcal' value=". round($kcal, 1) ." disabled>";
} 

Can't find their the problem is. Maybe POST can't receive information that was send via ajax ?

Kintamasis
  • 265
  • 6
  • 27

3 Answers3

0

Try setting the Content Type in your Ajax POST:

$.ajax({
    type: 'post',
    url: 'ajaxLoader.php',
    data: {
        galutinis_kcal:totalPointsKcal,
    },
    contentType: "application/json",
    success: function (response) {
        document.getElementById("galutinis").innerHTML=response;
        $('.kcal1').trigger('change');
    }
});

If you don't set the content type of the Ajax call the body (data) won't be sent.

docksteaderluke
  • 2,195
  • 5
  • 27
  • 38
  • not working, now the input field disappears and ajax loader not return new field. – Kintamasis Apr 12 '16 at 16:39
  • Can you verify that $_POST["galutinis_kcal"] is still not set? You may also need to add `dataType: "text/html"` to your ajax request. `dataType` is the content type that your ajax call expects from the server. – docksteaderluke Apr 12 '16 at 16:45
  • Without dataType and contentType I receive information and new input with counted value. But with dataType and contentType set it didn't enter success function, I just check with alert(); – Kintamasis Apr 12 '16 at 16:54
0

At the server side use this code to get data sent via AJAX Request:

$info = json_decode(file_get_contents("php://input"));

if(isset($info->galutinis_kcal)){
    $kcal = $info->galutinis_kcal;
    echo "<input class='skaiciuokle kcal1' type='text' name='g_kcal' value=". round($kcal, 1) ." disabled>";
}

at the Client side use JSON.stringify before send your request

$.ajax({
    type: 'post',
    url: 'ajaxLoader.php',
    data: JSON.stringify({
        galutinis_kcal:totalPointsKcal
    }),
    success: function (response) {
        document.getElementById("galutinis").innerHTML=response;
    }
});
Jose Rojas
  • 3,490
  • 3
  • 26
  • 40
  • it returned new input value, but when I save, I still receive Notice: Undefined index: – Kintamasis Apr 12 '16 at 17:00
  • The php file returned the value?, where are you saving? – Jose Rojas Apr 12 '16 at 17:03
  • know I'm in test.php there are ajax request, javascript code. In test.php then input change, the code above count value and send to ajaxLoader.php and it innerHTML the #galutinis. And i see value in input. Then i click button to save. Form method="test.php" return in same page. And then I receive Notice: Undefined index: g_kcal in – Kintamasis Apr 12 '16 at 17:07
0

The problem was input field was set to DISABLE. Now it's working fine

Kintamasis
  • 265
  • 6
  • 27