我使用ajax创建了一个表单,它将数据作为json数组发布到单独的php文件中。用于将表单发布到php处理程序的标记ajax是:
$.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//
}
});
这将传递给url中指定的文件ajax.php,它使用php验证检查错误,如果一切正常,则应该插入到数据库中。该部分的编码标记是:
<?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);
?>
出于某种原因,它没有从浏览器发布任何值到php文件,并且显示一个空白数组。当我使用'get'类型时,它可以工作。任何人都可以帮忙告诉我为什么它不适用于'post'类型?如果需要ajax.php部分,请告诉我......
答案 0 :(得分:1)
$HTTP_POST_VARS
is deprecated(您应该已经停止使用十一年前)并在我的PHP副本上使用它进行快速测试无法使其正常工作。使用$_POST
。
答案 1 :(得分:1)
使用.serializeArray()而不是.serialize来做你想要的。
http://api.jquery.com/serializeArray/
示例结果:
[{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/
示例结果:
a=1&b=2&c=3&d=4&e=5
代码应该更接近这一点。使用.done
并捕获函数中的返回数据。
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);
?>