How to pass a Javascript value to a PHP form, and send it as a POST method?

1

How to pass a Javascript value to a PHP form and send it as a POST method?

I have a script:

if (cc.isValid()) { 
    $('#debug').text(cc.hash());             
    var verifica = cc.hash();
    //  $('#debug').text(verifica);      
    console.log(verifica);

    alert('Pegou o verifica: '+verifica)
}

I need to get the value of "verify" and send along with an HTML + PHP form as this POST Javascript value. How can I do it?

    
asked by anonymous 29.07.2018 / 21:25

1 answer

2

Create a input type hidden in the form (anywhere within your <form></form> ) with name "check":

<input type="hidden" name="verifica">

With your jQuery you change the value of input to the value of the variable:

if (cc.isValid()) { 
   $('#debug').text(cc.hash());             
   var verifica = cc.hash();
   //  $('#debug').text(verifica);      
   console.log(verifica);
   alert('Pegou o verifica: '+verifica)

   // insere o valor no campo
   $("input[name='verifica']").val(verifica);

   // ou
   // $("[name='verifica']").val(verifica);

In this way, when submitting the form, it will go next to the field with the value added.

    
29.07.2018 / 21:33