ajax表格没有提交帖子

I have created a form using ajax, which posts the data as a json array, to a seperate php file. The markup ajax for posting the form to the php handler is:

$.ajax({
   dataType: "json",
   type: "post",
   url: "ajax.php?action=add_driver",
   data: $("form#addDriver").serialize(), // serializes the form's elements.
   beforeSend: function()
   {

$('.error, .success, .notice').remove();

},
success: function(json)
{

if (json['status']=='success'){
    alert(json['message']);
    }else{

if(json['error']['driver_firstname']){
    $("input[name=driver_firstname]").after('<div class="error">'+json['error'['driver_firstname']+'</div>');   
}

if(json['error']['driver_surname']){
    $("input[name=driver_surname]").after('<div class="error">'+json['error']['driver_surname']+'</div>');  
}
if(json['error']['driver_dob']){
    $("input[name=driver_dob]").after('<div class="error">'+json['error']['driver_dob']+'</div>');  
}

if(json['error']['driver_address']){
    $("input[name=driver_address]").after('<div class="error">'+json['error']['driver_address']+'</div>');  
}

if(json['error']['driver_postcode']){
    $("input[name=driver_postcode]").after('<div class="error">'+json['error']['driver_postcode']+'</div>');    
}

if(json['error']['driver_city']){
    $("input[name=driver_city]").after('<div class="error">'+json['error']['driver_city']+'</div>');    
}

if(json['error']['driver_county']){
    $("input[name=driver_county]").after('<div class="error">'+json['error']['driver_county']+'</div>');    
}

if(json['error']['driver_email']){
    $("input[name=driver_email]").after('<div class="error">'+json['error']['driver_email']+'</div>');  
}

if(json['error']['driver_tel']){
    $("input[name=driver_tel]").after('<div class="error">'+json['error']['driver_tel']+'</div>');  
}

if(json['error']['driver_mobile']){
    $("input[name=driver_mobile]").after('<div class="error">'+json['error']['driver_mobile']+'</div>');    
}   

//These are the series of validation checks carried out before submission//


}
                     });

This is passed to the file named in the url, ajax.php, which checks for errors, using the php validation, and if all is ok, is supposed to insert into the database. The coding markup for this part is:

<?php
$json = array();


if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

//for the first field only//            
$driver_firstname = tep_db_prepare_input($HTTP_POST_VARS['driver_firstname']);

if (strlen($driver_firstname)<4){
  $json['error']['driver_firstname'] = 'First Name is required!';
}

if (isset($json['error']) and !empty($json['error'])){

    $json['status'] = 'error';
    $json['message'] = 'Please check your error(s)!';

}else{


     $sql_data_array = array('driver_firstname' => $driver_firstname);

     tep_db_perform(TABLE_DRIVERS, $sql_data_array);

     $driver_id = tep_db_insert_id();

     $json['status'] = 'success';
     $json['message'] = 'Data has been successfully updated!';
    }

}

     echo json_encode($json);

?>

For some reason, it is not posting any of the values to the php file from the browser, and is showing a blank array. When I use the 'get' type, it works. Can anyone help to tell me why it is not working for the 'post' type? If the ajax.php part is required, please let me know...

$HTTP_POST_VARS is deprecated (you should have stopped using it over eleven years ago) and a quick test with it on my copy of PHP failed to get it to work. Use $_POST.

Use .serializeArray() instead of .serialize to do what you want.

http://api.jquery.com/serializeArray/

Sample Result:

[{name: "a", value: "1" }, {name: "b", value: "2" }, {name: "c", value: "3"}, {name: "d", value: "4" }, {name: "e", value: "5" }]

http://api.jquery.com/serialize/

Sample Result:

a=1&b=2&c=3&d=4&e=5

Code should look closer to this. Use .done and catch return data in the function.

JS:

$.ajax({
    type: "POST", 
    url: "ajax.php?action=add_driver", 
    dataType: "json",
    date:  $("form#addDriver").serializeArray(),
    beforeSend: function() {
        $('.error, .success, .notice').remove();
    }
}).done(function(json) {
    if (json['status']=='success'){
        alert(json['message']);
    }else{
        if(json['error']['driver_surname']){
           /* Do Stuff */ 
        }
    }
});

PHP:

<?php
$myData = array();
$json = array();

if(isset($_POST['yourData'])){
    $myData = $_POST['yourData'];
}

/* Do stuff */
$json['myValue'] = $myData['passedValue']; 


echo json_encode($json);
?>