我对jQuery和AJAX技术非常了解。我正在尝试创建一个表单,它将在数据库中插入值而不刷新页面。大多数代码都按预期工作。但是有两个案例没有按预期运作。
请查看以下相应代码。
Jquery:
$('#add').click(function(){
var domain_type = $('#domain').val();
var domain_zone = $('#domain_zone').val();
var client_name = $('#client_name').val();
//var language[] = "";
var lang = new Array();
if(language==1){
//alert("You select one language");
valid = true;
lang = "English";
}else{
//alert("You select multiple language");
var i =0;
$.each($('input[name=lang]:checked'), function(){
lang.push($(this).val());
});
var count = lang.length;
if(count ==1){
alert("You need to select two languages");
var valid = false;
}else if(count>2){
alert("You can select only two languages");
var valid = false;
}else{
valid = true;
}
}
var formMsge = $('#formStatus');
$('#formStatus').html('<img src="./img/loader.gif"> Please wait while adding the data in database.');
$.ajax({
type: "POST",
url: "./test/test.php",
data: {domain_type:domain_type, domain_zone:domain_zone, client_name:client_name, 'lang[]':lang},
success:function(conf){
$('#formStatus').ajaxComplete(
function(event,request){
if(conf=="OK"){
$('#formSuccess').show();
$('#addClient').hide();
}else{
$('#formError').show();
$('#forError').text("Please try again");
}
}
);
}
});
return false;
});
test.php的:
include('../../classes/access_user/all.class.inc.php');
echo $workstream->addClient()
php类文件:
public function addClient(){
$domain_type = isset($_POST['domain_type'])?$_POST['domain_type']:'';
$domain_zone = isset($_POST['domain_zone'])?$_POST['domain_zone']:'';
$client_name = isset($_POST['client_name'])?$_POST['client_name']:'';
$lang = isset($_POST['lang'])?$_POST['lang']:'';
$add_by = 'Robindra Singha';
$add_on = date("Y-m-d");
if(isset($domain_type) || isset($domain_zone) || isset($client_name) || isset($lang)){
$sql = "INSERT INTO client_list (domain_type_id, client_name, domain_zone, language, add_on, add_by, last_update_on, last_update_by) VALUES('$domain_type','$client_name','$domain_zone','$lang','$add_on','$add_by','$add_on','$add_by')";
$res = mysql_query($sql) or die(mysql_error());
if(mysql_inserted_id() != 0){
$msg = "OK";
}else{
$msg = "System face an issue while adding client details!";
}
}else{
$msg = "System face an issue while adding client details, please try again!";
}
return $msg;
}
在上面的代码中,当我选择多个复选框时,我的问题是复选框值无法通过,第二个我无法在表单成功提交后显示任何文本。除了这两个问题,我的代码能够成功插入信息。如果有人指导我正确完成工作,我会很高兴。提前感谢您的支持。
注意:我无法在此粘贴时添加HTML文件,它显示为普通文本。请建议。