my question is: I am sending data through ajax to the server, for that I have a bit of code in php and js. but what happens is that when the data is correct it sends me to the php screen telling me that they are correct but it does not show me the image that I want to show in my html when the data is correct, I do not want to redirect me to any screen but I overwrite the image in my html.
<script>
$(document).on("ready", function () {
var pet = $('#main form').attr('action');
var met = $('#main form').attr('method');
$('#main form').on('submit', function (e) {
$.ajax({
breforeSend: function () {
$('#status').html('<img src="img/10165.png" alt="" height="25px" width="25px">');
},
url: pet,
type: met,
data: $('#main form').serialize(),
success: function (resp) {
if(resp == "Correcto")
$('#status').html('<img src="img/10165.png">');
else
console.log(resp)
},
error: function (jqXHR, estado, error) {
console.log(estado)
console.log(error)
},
complete: function (jqXHR, estado) {
cosole.log(estado)
},
timeout: 3000
})
})
})
</script>
and my function in php is:
<?php
$nombre = $_POST["nombre"];
$mail = $_POST["mail"];
sleep(4);
if($nombre != "" && $mail != "")
echo 'correcto';
else
echo "Incorrecto";
? >
Finally my code in html is:
<div id="main">
<h1>Formulario de contacto</h1>
<form action="peticion.php" name="fo" method="POST" autocomplete="on">
<input type="text" name="nombre" placeholder="Nombre..." >
<input type="text" name="mail" placeholder="Correo..." >
<div style="display:block;width:60%;margin:0 auto" >
<input type="submit" name="send" value="Enviar">
<div id="status" name="status"></div>
</div>
</form>
</div>
</body>