我面臨檢查數據庫中唯一電話號碼的問題。在提交時,它會檢測我輸入的每個數字作爲數據庫中發現的數字,即使它不存在。附接下面的代碼:Codeigniter:ajax中的唯一電話號碼檢查
AJAX
<script>
function checkdata(){
var tel = $('.tel').val();
var x;
if(tel){
$.ajax({
url: '<?php echo base_url('index.php/admin/customer/check_phone_availibility1'); ?>',
data:{tel:tel},
error: function() {
alert("An error has occurred.");
},
success: function(data) {
x= data;
},
type: 'POST'
});
if(!x){
alert('This phone exists in database');
$('.tel').val("");
return false;
}
}
}
</script>
控制器
public function check_phone_availibility1(){
$tel = $this->input->post('tel');
$success = $this->customer_m->check_phone_no_availability($tel);
if($success){
echo true;
}
else {
echo false;
}
}
MODEL
public function check_phone_no_availability($phone){
$query = $this->db->query("SELECT * from customers WHERE phone='$phone'");
$phone = $query->result();
if(count($phone))
{
return false;
}
else
{
return true;
}
}
VIEW
<input type="text" name="phone" value="" class="form-control tel" id="phone" maxlength="10" required="">
<input type="submit" name="submit_btn" value="Save Customer" class="btn btn-success" onclick="return checkdata();">
我將非常感激,如果這個問題能夠得到解決。由於
嘗試'如果(計數($電話)> 0)' – hungrykoala
嘿,我會努力它出來回覆你 –