Jquery does not pass the POST value of a form

0

Colleagues

I have a form that I'm using jquery to pass the values to, but the value does not pass to the send.php file. See:

    <form method="post" name="post" id="contact-form">  
                    <div id="success"></div>
                    <label for="exampleInputEmail1">Matéria:</label>
                    <div class="input-group">
                      <div class="input-group-addon">
                        <i class="fa fa-arrow-circle-right" aria-hidden="true"></i>
                      </div>
                      <input type="text" class="form-control" name="NovaMateria" maxlength="150" id="tirarEspacos">
                    </div>
    </form>   

<script type="text/javascript">    
    $('#submit').click(function() {
      $.post("enviar.php", $(".contact-form").serialize(), function(response) {
        $('#success').html(response);
    $('#tirarEspacos').val('');     
      });
      return false;
    });
  </script>

send.php

<?php 
echo $_POST['NovaMateria'];
    
asked by anonymous 27.01.2017 / 17:07

2 answers

4

In this section you are trying to recover an element incorrectly, . indicates the class of the element, so nothing is serialized.

$.post("enviar.php", $(".contact-form").serialize(), function(response) {
                       ^^^

Try replacing with the ID indicator, # , as in the example below:

$.post("enviar.php", $("#contact-form").serialize(), function(response) {
                       ^^^
    
27.01.2017 / 17:17
3

In addition to being incorrectly recovering $(".contact-form").serialize() instead of $("#contact-form").serialize() since in the form tag used id and not class, there is also no button to submit the data. The code would look like this:

HTML code:

<form method="post" name="post" id="contact-form">  
      <div id="success"></div>
      <label for="exampleInputEmail1">Matéria:</label>
      <div class="input-group">
        <div class="input-group-addon">
           <i class="fa fa-arrow-circle-right" aria-hidden="true"></i>
        </div>
        <input type="text" class="form-control" name="NovaMateria" maxlength="150" id="tirarEspacos">
     </div>
     <button id="submit">Enviar</button>
</form>  

Jquey code:

<script type="text/javascript">    
    $('#submit').click(function() {
      $.post("enviar.php", $("#contact-form").serialize(), function(response) {      
        $('#success').html(response);
        $('#tirarEspacos').val('');  
      });
      return false;
    });
 </script>
    
27.01.2017 / 17:31