简体   繁体   中英

$_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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM