skip to Main Content

This form was working till jan/24. Now, all variables are empty on my $_POST.

Here is my code:

HTML:

<form id="main-schedule-form" name="schedule-form" method="post" action="sendemailschedule.php">                            
    <label for="nome">Nome*</label>
    <input type="text" class="form-control" id="nome" name="name" required/>
    <label for="email">E-mail*</label>
    <input type="email" class="form-control" id="email" name="email" required/>
    <button type="submit" class="btn btn-primary btn-lg">Enviar</button>
</form> 

Jquery:

var form = $('#main-schedule-form');
form.submit(function(event) {
   var str = $(this).serialize(); // when I console.log it, the content is right
   $.ajax({
      url: $(this).attr('action'),
      type: "POST",
      data: str,
   }).done(function(data) {
      $('#main-schedule-form')[0].reset();
      alert("Thanks!");
   });
});

PHP:

<?php
    $name = @trim(stripslashes($_POST['name'])); 
    $email= @trim(stripslashes($_POST['email']));
    ...

when I print $name and $email, it gets empty. When I var_dump($_POST), I receive an empty array.

What do I miss?

2

Answers


  1. Chosen as BEST ANSWER

    The problem was on my .htaccess. The $_SERVER["REQUEST_METHOD"] was returning GET because of it


  2. As it turns out, there was an issue in .htaccess that prevented this from working. .htaccess does not change the request method, so if it was due to something in the .htaccess, then it must have been a redirect to a GET request.

    Your request type is indeed set to POST and you indeed pass the parameters correctly, this is why it was working up to pretty recently.

    var form = $('#main-schedule-form');
    form.submit(function(event) {
       var str = $(this).serialize(); // when I console.log it, the content is right
       console.log(str);
       $.ajax({
          url: $(this).attr('action'),
          type: "POST",
          data: str,
       }).done(function(data) {
          $('#main-schedule-form')[0].reset();
          alert("Thanks!");
       });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <form id="main-schedule-form" name="schedule-form" method="post" action="sendemailschedule.php">                            
        <label for="nome">Nome*</label>
        <input type="text" class="form-control" id="nome" name="name" required/>
        <label for="email">E-mail*</label>
        <input type="email" class="form-control" id="email" name="email" required/>
        <button type="submit" class="btn btn-primary btn-lg">Enviar</button>
    </form>

    So, it’s important to realize that .htaccess does redirects. If you watch the Network tab of your browser’s dev tools, then you should see these parameters being properly passed to a POST request, just like you expected. So it’s not your Javascript nor your PHP that was to blame, but some erroneous changes on your htaccess.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search