Error with serialize() + PHP function

Asked

Viewed 84 times

0

I’m using the serialize function to send data from a form to a PHP page:

index php.

<form id="formfeira" method="post">

      <div class="form-group">
        <label for="horario">Horário de funcionamento:</label>
        <input type="text" class="form-control" id="horario" name="horario" placeholder="das 8h às 18h">
      </div>

      <div class="form-group">
        <label for="pagamento">Formas de pagamento:</label>
        <input type="text" class="form-control" id="pagamento" name="pagamento" placeholder="à vista ou nos cartões Visa e Mastercard">
      </div>

      <div class="form-group">
        <label for="quem">Quem somos:</label>
        <textarea class="form-control" id="quem" name="quem" rows="3" placeholder="descreva aqui..."></textarea>
      </div>

      <button type="submit" class="btn btn-primary" onclick="salvaFeira()">Enviar dados</button>
    </form>

functions js.

$(document).ready(function(){});
function salvaFeira(){
  alert($("#formfeira").serialize()); //aqui funciona
  $.ajax({
    type: "POST",
    url: "salvarFeira.php",
    data: $("#formfeira").serialize(),
    success: function(data) {
      alert(data); //não chega aqui
    }
  });
}

saveFeira.php

<?php

include 'conexao.php';

$mysqli = new mysqli("localhost", "root", "", "feira");
$res = $mysqli->query("insert into feira (horario, formapagamento, quemsomos) values ('123', '456', '789')");

echo 'teste'; 
?>

I gave up trying to rescue the data and am just making a common Sert and returning the word test

However, it is not saving in the database. If I directly execute the file save.php, it saves the data in the database correctly.

What am I doing wrong?

  • 1

    In browser developer tools (F12, usually), in the Network tab you can see all requests made on the page. See if the POST request is being made and if so, select it with the mouse and see the answer obtained.

  • great, I could find out. error 404, missed include the folder where the file is. thanks even, I was all afternoon cracking my head with this.

  • @Andersoncarloswoss did the test now and saved in the good bank. however the line Alert(data) of the js file is not yet running.

  • Did you check if the answer was 200? I already had problems with the configuration of the PHP server that did not understand that the file had run successfully and needed to set the code manually with the http_response_code()

  • @Andersoncarloswoss reply was 200 yes

  • @Andersoncarloswoss took a closer look at the Network screen and it looks like this: in Params appear the form data. In Response appears the html code of the index page

  • Da Index? Do you have any . htaccess in the project that might be redirecting the request?

  • @Andersoncarloswoss I don’t know, I’m using wamp, how do I know?

  • See if there is the . htaccess file in the project directory. I don’t remember if it is visible by default on Windows, but on Linux it is a hidden file.

  • there is, in the project directory only bootstrap, index, js and php files

  • @Andersoncarloswoss added jquery code error: function(xhr, status, error) { console.log(status, error);//Captura o erro e envia ao console } and it is returning 'error', but does not specify what the error is. how do I make it tell which error is occurring?

Show 6 more comments

1 answer

1


I copied your code and tested, what’s wrong is you set the type of the element button as Submit, to perform an ajax request, the property should actually be set as BUTTON as below:

<button type="button" class="btn btn-primary" onclick="salvaFeira()">Enviar 
dados</button>

While it was like Submit didn’t work because the page reloaded and missed the request sent by ajax.

inserir a descrição da imagem aqui

In my test now works perfect, and for you ?

PS: Don’t forget that jquery must be included on your page to make it work:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>

Hugs...

Browser other questions tagged

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