I'm trying to submit a simple form to a database using a little PHP script and it worked perfectly.
div id="prontoform">
<!--<p>*</p>-->
<form action="procesar.php" method="post">
<input type="text" class="input" value="" name="nombre" placeholder="nombre"></input>
<input type="text" class="input" value="" name="mail" placeholder="mail"></input>
<input type="submit" value="Enviar" id="enviar"></input>
</form>
</div>
but when i try to serialize it trough ajax is not working anymore, i get a new entry in my db but its empty and also the script returns me my own error message i dont know why
jQuery(function($) {
$("#enviar").click(function(e) {
e.preventDefault();
var opciones = {
method:"post",
url:"procesar.php",
data: $('form').serialize(),
success:function(result){
$("body .exito").remove();
$("body .error").remove();
if(result=="exito"){
$("body").append("<div class='exito'>Sus datos fueron enviados correctamente.</div>");
}else{
$("body").append("<div class='error'>Error al enviar los datos, por favor intente denuevo.</div>");
}
}
};
$.ajax(opciones);
});
});
any clue on what's going on?
EDIT: this is the php script, as i said before it worked well without ajax.
<?php
if(isset($_POST['nombre'])){
$nombre = $_POST['nombre'];
}else{
$nombre = "";
}
if(isset($_POST['mail'])){
$mail = $_POST['mail'];
}else{
$mail = "";
}
$conn=mysql_connect('localhost', 'viveofer', '0Q51akeSf1');
mysql_select_db("viveofer_1",$conn);
$query ="INSERT INTO datos_pronto (nombre, mail)";
$query .="VALUES ('".$nombre."','".$mail."')";
mysql_query($query) or die("Error sending data.<br>");
return "exito";
?>
You can use following demo code Form id=frm
$("#frm").ajaxform(function(){
//here you can write your ajax before send,suceess and complete methods
}).submit();
You need to add jquery form
Use $('form', '#prontoform')
instead of $('form')
in data: $('form').serialize(),
..
There could be other forms in the page, by using $('form')
, you might be targeting other forms.. By using $('form', '#prontoform')
you'll be using form inside div #prontoform
..
EDIT: As per the discussions the problem is with the line method:"post",
it should be type:"post",
Use this
$('#prontoform form').serialize();
Instead of this
$('form').serialize()