Issue
I want to make a judge button to judge the value entering from users whether the users used or not. But I faced some problem , I want to count the value when I got return data from the database with knex SQL Builder, I found the return data was [ { ‘count(Program_Codename
)’: 1 }].
How can I get the value only count number ?
this is my backend code:
router.post('/',function(req,res){
console.log(req.body.Program_name);
db('program_detail').count('Program_Codename').where('Program_Codename',req.body.Program_name).asCallback(function(err,values){
if(err){
console.log(err);
}else{
console.log(values);
if(values > 0){
res.json({success:1});
//console.log(req.body.Program_name);
console.log("This Program_Codename has already been used.")
}else{
console.log("This Program_Codename can be used.")
}
}
db.destroy();
});
});
module.exports = router;
Moreover, I want to return the judgement to the front-end website to tell users the results with ajax , how can I do ?
this is my front-end ajax code:
$(document).ready(function(){
$("#checkname_btn").click(function(){
console.log("test");
var Program_name = { Program_name : $(':input[name=Program_Codename]').val()};
$.ajax({
data: Program_name,
url: '/check',
dataType: 'json',
type:'post',
cache: false,
timeout: 5000,
success: function(data){
var data = $.parseJSON(data);
},
error: function(jqXHR, textStatus, errorThrown){
alert('error ' + textStatus + " " + errorThrown);
}
});
//$(':input[name=Program_Codename]').next().next().after('<div id = "id" align="center">'+message+'</div>');
//$(':input[name=Program_Codename]').focus(function(){
//$('#id').remove();
return false;
});
});
The comment out code is the outcome I want to make, but I don’t know how to do it. I want to make realtime feedback to users under the judge button.
By the way, I can got the value passed from the front-end.
My button code:
<td class="td-edit">Program/Codename:</td>
<td class="td-edit">
<input class="form-control-edit" type="text" name="Program_Codename">
<br><button class="btn btn-primary" id=checkname_btn>Check Program/Codename</button>
</td>
Solution
Get count by this code:
var count = values[0]['count(Program_Codename)'];