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?
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.
– Woss
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.
– Italo Rodrigo
@Andersoncarloswoss did the test now and saved in the good bank. however the line Alert(data) of the js file is not yet running.
– Italo Rodrigo
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()
– Woss
@Andersoncarloswoss reply was 200 yes
– Italo Rodrigo
@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
– Italo Rodrigo
Da Index? Do you have any . htaccess in the project that might be redirecting the request?
– Woss
@Andersoncarloswoss I don’t know, I’m using wamp, how do I know?
– Italo Rodrigo
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.
– Woss
there is, in the project directory only bootstrap, index, js and php files
– Italo Rodrigo
@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?– Italo Rodrigo