if (isset($_POST['send'])) does not work with Ajax

Asked

Viewed 400 times

0

I am sending information to another page via Ajax:

<script type="text/javascript">
  //Função para enviar as informações para o arquivos processa.php 
  //via Ajax
  function envia(x){


      jQuery('#form_protocolo_' + x).submit(function(){
        var dados = jQuery( this ).serialize();

        alert(dados);

        jQuery.ajax({
          type: "POST",
          url:  "processa.php",
          data: dados,
          success: function( data )
          {
            document.getElementById('resultado').innerHTML = ( data );
          },
          error: function(data, textStatus){
            alert( textStatus );
          },

        });
        return false;
      });
  }
</script>

However, my file protocolo.php, who receives the information has a verification: if (isset($_POST['enviar'])). If the user clicks the send button, the file continues the action.

But when I’m getting the answer in success: function( data ) it behaves as if the user had not pressed the Send button, returning me Você não clicou no botão enviar!, even though this one clicked on it.

if (isset($_POST['enviar'])){

  //Executa a ação

} else {

  echo "Você não clicou no botão enviar!";

}

What can it be?

  • Pole or pole HTML too, please.

  • 1

    First: in AJAX you send the information to processa.php, but cites a file protocolo.php. Is that all right? Second: when you make an AJAX request, the button field is not included, as it should only be included by the browser when processing the event submit form. You will need to review your condition in PHP.

  • 1

    If sending is a button its value will only be sent if it has the attribute name is set, which is not the best way to do it. To find out if a submission of the POST better test with if ($_SERVER['REQUEST_METHOD'] == 'POST') and then test each field individually

  • I tested with if ($_SERVER['REQUEST_METHOD'] == 'POST') and it worked. I got the result I expected. I will follow your tips to work in the best way possible. Thank you!

1 answer

2


You are searching for the "Submit" Index in the Global POST array, but this Index has not been defined at any time, this is in your Html as the name of one of the fields in your form ? Make an Isset of any of the form fields and remember to assign a name to all of them and do Isset from Name and not from ID or other attribute.

  • Yes Yes. All are made from the name attribute. <button type='Submit' onclick='send($id_protocolo_doc);' name='send' value='send' style='display:None;' id='botao_receber_$id_protocolo_doc' >RECEIVE</button>

Browser other questions tagged

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