Capture, in PHP page, the value of an input that was changed at runtime

0

The JS below captures the value of the URL and assigns it to value of input .

var valor_num = /num_nfe=([^&]+)/.exec(window.location.href)[1];

//console.debug("Num. NFe: "+$("[name='num-nfe']").val());
//console.debug("Form: "+ JSON.stringify($('#meu_form').serializeObject()));

$.post("actions/autosalvar.php", function (data) {
            $("[name='cliente']").attr('value', data.cliente);
            $("[name='id-cliente']").attr('value', data.id_cliente);
            $("[name='tipo-pessoa']").attr('value', data.tipo_pessoa);
            $("[name='num-nfe']").attr('value', valor_num);
}, "json");

setInterval(function () {     
                var dados = $('#meu_form').serializeObject();
                $.post("actions/autosalvar.php", 
                {'meus_dados': dados}).done(function( data ){});
}, 2000);

Initially, when attempting to capture this value on page autosalvar.php , it is not set, and is only available in a second run.

1st page execution autosalvar.php

Array
(
    [cliente] => João
    [id-cliente] => 2
    [tipo-pessoa] => PF
)

2nd run [after 2 second interval] of page autosalvar.php

Array
(
    [cliente] => João
    [id-cliente] => 2
    [tipo-pessoa] => PF
    [num-nfe] => 59
)
    
asked by anonymous 22.02.2017 / 19:57

1 answer

0

The solution was as follows:

→ Pass as argument the value of input , in the first request $.post :

var val_num = $("[name='num-nfe']").val();
$.post("actions/autosalvar.php", {'val_num': val_num}).done(function (data) {
...
}, "json");

→ Now in PHP you receive the data like this:

if(isset($_POST['val_num'])){ // 1ª instância do $.post
   $valor = $_POST['val_num'];
}else{
   $valor = &$_POST['meus_dados']['num-nfe']; // Outras instâncias
}
    
23.02.2017 / 03:44