我有一个包含3个表国家,州和城市的数据库工作正常。
问题是我有一个表格,我可以选择这三个没有问题,但在选择并存储选定的值后,它存储数字ID而不是这三个的名称
选择的php部分:
General error: 1364 Field 'image' doesn't have a default value (SQL: insert into `product_images` (`product_id`) values (21))
JS部分:
<div class="section colm colm6">
<label class="field prepend-icon select">
<select name="country" class="countries" id="countryId">
<option value="">Select Country</option>
</select>
<span class="field-icon"><i class="fa fa-globe"></i></span>
<i class="arrow double"></i>
</label>
</div>
答案 0 :(得分:2)
如果你想要这个名字,你应该更改你的选项attr line:
option.attr('value', val).text(val);
答案 1 :(得分:1)
你用什么来存储在db中。??以上代码仅用于绑定。 检查是否保存文本或值?
您为绑定提供的代码看起来是正确的。
答案 2 :(得分:0)
来自@ piyushanwekar和@HudsonPereira的输入
我做了一些更改,并制作了以下适用于我的代码
function ajaxCall() {
this.send = function(data, url, method, success, type) {
type = type||'json';
var successRes = function(data) {
success(data);
}
var errorRes = function(e) {
console.log(e);
//alert("Error found \nError Code: "+e.status+" \nError Message: "+e.statusText);
//$('#loader').modal('hide');
}
$.ajax({
url: url,
type: method,
data: data,
success: successRes,
error: errorRes,
dataType: type,
timeout: 60000
});
}
}
function locationInfo() {
var rootUrl = "../api.php";
var call = new ajaxCall();
this.getCities = function(id) {
$(".cities option:gt(0)").remove();
var url = rootUrl+'?type=getCities&stateId=' + id;
var method = "post";
var data = {};
$('.cities').find("option:eq(0)").html("Please wait..");
call.send(data, url, method, function(data) {
$('.cities').find("option:eq(0)").html("Select City");
if(data.tp == 1){
$.each(data['result'], function(key, val) {
var option = $('<option />');
option.attr('value', val).text(val);
option.attr('cityid', key);
$('.cities').append(option);
});
$(".cities").prop("disabled",false);
}
else{
alert(data.msg);
}
});
};
this.getStates = function(id) {
$(".states option:gt(0)").remove();
$(".cities option:gt(0)").remove();
var url = rootUrl+'?type=getStates&countryId=' + id;
var method = "post";
var data = {};
$('.states').find("option:eq(0)").html("Please wait..");
call.send(data, url, method, function(data) {
$('.states').find("option:eq(0)").html("Select State");
if(data.tp == 1){
$.each(data['result'], function(key, val) {
var option = $('<option />');
option.attr('value', val).text(val);
option.attr('stateid', key);
$('.states').append(option);
});
$(".states").prop("disabled",false);
}
else{
alert(data.msg);
}
});
};
this.getCountries = function() {
var url = rootUrl+'?type=getCountries';
var method = "post";
var data = {};
$('.countries').find("option:eq(0)").html("Please wait..");
call.send(data, url, method, function(data) {
$('.countries').find("option:eq(0)").html("Select Country");
console.log(data);
if(data.tp == 1){
$.each(data['result'], function(key, val) {
var option = $('<option />');
option.attr('value', val).text(val);
option.attr('countryid', key);
$('.countries').append(option);
});
$(".countries").prop("disabled",false);
}
else{
alert(data.msg);
}
});
};
}
$(function() {
var loc = new locationInfo();
loc.getCountries();
$(".countries").on("change", function(ev) {
var countryId = $("option:selected", this).attr('countryid');
if(countryId != ''){
loc.getStates(countryId);
}
else{
$(".states option:gt(0)").remove();
}
});
$(".states").on("change", function(ev) {
var stateId = $("option:selected", this).attr('stateid');
if(stateId != ''){
loc.getCities(stateId);
}
else{
$(".cities option:gt(0)").remove();
}
});
});