Using example from here http://www.htmlblog.us/jquery-autocomplete
I modified example and got such working code
$(document).ready(function () {
$('.zipsearch').autocomplete({
source: 'suggest_zip.php',
minLength: 2,
select: function (event, ui) {
ui.item.value = ui.item.account_number;
// when a zipcode is selected, populate related fields in this form
//this.form.account_number.value = ui.item.account_number;
this.form.account_description.value = ui.item.account_description;
this.form.account_number.value = ui.item.account_number;
}
}); //$('.zipsearch').autocomplete({
});
html
<form onsubmit="return false;">
<input type="hidden" id="additional_value" value="1" />Enter a Zipcode:
<input id="zipsearch" type="text" class="zipsearch" />
<br>Description of account:
<input id="account_description" type="text" disabled />
<br>Account number:
<input id="account_number" type="text" disabled />
</form>
With this code if I enter something in <input id="zipsearch" type="text" class="zipsearch" />
then entered value is passed to external php, processed and returned back. So far ok.
But need to send to external php also value from <input type="hidden" id="additional_value" value="1"/>
Found example How do I pass an extra parameter to Jquery Autocomplete field?
Tried to modify and get
$('.zipsearch').autocomplete({
source: function (request, response) {
$.getJSON("suggest_zip.php", {
additional_value: $('#additional_value').val()
}, response);
},
minLength: 2,
select: function (event, ui) {
ui.item.value = ui.item.account_number;
this.form.account_description.value = ui.item.account_description;
this.form.account_number.value = ui.item.account_number;
}
});
But no response from external php
Please, advice what need to correct?
Again tried to modify and get
$("#zipsearch").autocomplete
({
source:"suggest_zip.php?postcode=" + $('#additional_value').val() +"&", minLength: 2,
select: function(event, ui){
ui.item.value=ui.item.account_number;
this.form.account_description.value = ui.item.account_description;
this.form.account_number.value = ui.item.account_number;
}
});
this partly works. But how I can access to additional_value
in external php? Tried for example 'account_description' => $_POST['additional_value']
get blank (empty)
external php is like this
$to_execute = $_REQUEST['term']. '%';
try {
$stmt = $db->prepare('SELECT AccountNumber, DescriptionOfAccountLatvian, Number FROM 1_1_chartofaccounts WHERE AccountNumber LIKE ? ORDER BY AccountNumber ASC LIMIT 0,10');
$stmt->execute( array($to_execute) );
$array_for_autocomplete = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $ex) {
//echo "An Error occured!"; //user friendly message
print "Error!: " . $ex->getMessage() . "<br/>";
//some_logging_function($ex->getMessage());
exit;
}
if( isset($array_for_autocomplete) ){
$data = array();
foreach($array_for_autocomplete as $key => $row) {
$data[] = array(
'label' => $row['AccountNumber'] .', '. $row['DescriptionOfAccountLatvian'] .' '. $row['Number'] ,
'account_number' => $row['AccountNumber'] ,
'account_description' => $_POST['additional_value']
);
}
echo json_encode($data);
flush();
}//if( isset($array_for_autocomplete) ){