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

Asked

Viewed 29 times

1

How to pass a Javascript value to a PHP form and submit 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 take the value of "check" and send along to an HTML + PHP form as a POST method this Javascript value. How can I do?

1 answer

2


Create a input guy hidden on the form (anywhere within your <form></form>) with the name "checking":

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

With your jQuery you change the value of input with 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);

This way when submitting the form, you will join the field with the added value.

  • 1

    It worked. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.