I got stuck at while implementing jquery autocomplete with ajax the same fuction works for another table but in 2 tables(sales_man, Cashcustomer) its returing empty array but the fist table(customerMaster) it works fine and listing every cust as its request. My code
myApp.js
$('#custName').autocomplete({
minLength: 1,
autoFocus:true,
source: function( request, response ) {
$.ajax({
url: "/test/ajax/ajaxOpRespond.php",
data: {action:'Get_CashCust_Code', code:request.term},
dataType:'JSON',
type: "POST",
success: function( data ) {
var result = [];
if(data.length > 0){
for(var i=0;i<data.length;i++){
result[i] = { "value" :data[i].CUST.CUST_NAME+" ("+data[i].CUST.TELE_NO+")",
"custName":data[i].CUST.CUST_NAME,
"custtel1":data[i].CUST.TELE_NO,
};
}
}
else{
alert("jesha mesha");
$('#custType').val("new");
}
response(result);
},
error: function(xhr, textStatus, errorThrown){
alert("fail"+errorThrown);
var result = [];
response( result );
}
});
},
select: function( event, ui ) {
$("#custName").val(ui.item.custName);
$('#custTel1').val(ui.item.custtel1);$('#custTel1').attr("readonly","true");
$('#custType').val("old");
return false;
}
});
ajaxController.php
case 'Get_CashCust_Code':
$code = $_POST['code'];
$msql = "SELECT [ID]
,[CUST_NAME]
,[TELE_NO] FROM [dbo].[tbl_CashCustomer_Master] WHERE [CUST_NAME] LIKE '%{$code}%' ;";
$stmt = sqlsrv_query( $conn, $msql);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
if( sqlsrv_fetch( $stmt ) === false || sqlsrv_has_rows( $stmt ) === false) {
die( print_r( sqlsrv_errors(), true));
}
$rows = array();
while( $r = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
$rows[] = array('CUST' => $r);
}
sqlsrv_free_stmt( $stmt);
echo json_encode($rows);
break;
on ajax request it just return '[]'
please help
After hours of search and tries, i found my issue when i posted my question i was unaware of what was the real issue here and why it returns empty, well my actual problem was the first row is excluded from the response array when i added the same customer 2 times only one was listed on autocomplete this is because i executed and fetched the query 2 times at the same time
case 'Get_CashCust_Code':
$code = $_POST['code'];
$msql = "SELECT [ID]
,[CUST_NAME]
,[TELE_NO] FROM [dbo].[tbl_CashCustomer_Master] WHERE [CUST_NAME] LIKE '%{$code}%' ;";
$stmt = sqlsrv_query( $conn, $msql);
if( $stmt === false ) {
die( print_r( sqlsrv_errors(), true));
}
/*sqlsrv_fetch( $stmt ) is fetching all rows */
if( sqlsrv_fetch( $stmt ) === false || sqlsrv_has_rows( $stmt ) === false) {
die( print_r( sqlsrv_errors(), true));
}
$rows = array();
/*sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) fetching rows for the second time*/
while( $r = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) {
$rows[] = array('CUST' => $r);
}
sqlsrv_free_stmt( $stmt);
echo json_encode($rows);
break;
By reading the solution here he mentioned the mechanism of execution i understood when while is executing my pointer starts from 1 instead 0
Quoting his answer to that problem below
The sqlsrv_fetch_array() method retrieves a record and advances the cursor by one. You're looping through your records on line 190 in your while loop, but on line 21 you have this:
$auditData = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC),
so you have already fetched the first record. By the time you hit the loop on line 190, you're already on record 2.
You have a couple of options. One is to change your while loop to a do/while loop:
do { ... } while ($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
You would need to change line 21 (and associated rows) to be $row = ... instead of $auditData = ...
Alternatively, you could reset your pointer before starting your main loop. Can't remember how to do that with the SqlServer driver, but a quick google should set you on the right track.
I actually tired do while and same issue that's when I changed
if( sqlsrv_fetch( $stmt ) === false || sqlsrv_has_rows( $stmt ) === false) {
die( print_r( sqlsrv_errors(), true));
}
to
/* removed the first fetch */
if(sqlsrv_has_rows( $stmt ) === false) {
die( print_r( sqlsrv_errors(), true));
}
and now its working fine as expected, And thanks for your help and @Psi Thanks for notifying me of the SQL injection attack
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.