skunk a skunk a - 7 months ago 28
PHP Question

$_POST variables are not set when validate a form with jQuery ajax function

I have a html form and a jQuery function to validate my form.

When I send my form values to my php files,

$_POST
var is not set?

<form id="signup" class="dialog-form" action="bat/new_user.php" method="post">
<div class="form-group">
<label>E-mail</label>
<input type="text" name="mail" placeholder="email@domain.com" class="form-control">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" placeholder="My secret password" class="form-control">
</div>
<input type="submit" class="btn btn-primary">
</form>


PHP script is:

if (isset($_POST["mail"]))
{
$mail = $_POST["mail"];
print $mail;}
else{print "ko";
}


JS script is :

$( "#signup" ).submit(function( event ) {
event.preventDefault();
var $form = $( this ), url = $form.attr( "action" );
console.log($form.serialize() );
var posting = $.post( url, { s: $form.serialize() } );
posting.done(function( data ) {
$( "#register-dialog" ).append( data );
});
});


How I can get the form values?

Answer

The problem is the data passed to post(), it should be

var posting = $.post(url, $form.serialize());

You are passing a parameter called s with value as the serialized form