1

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) ){
Community
  • 1
  • 1
Andris
  • 1,434
  • 1
  • 19
  • 34

1 Answers1

1

In your modification you did "suggest_zip.php?postcode=" + $('#additional_value').val() In your php code use $_GET['postcode'] (because you are setting the $_GET variable as postcode)

If you added data to the autocomplete:

$(document).ready(function () {
    $('.zipsearch').autocomplete({
        source: 'suggest_zip.php',

        data: { 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;
        }
    });
});

Then you would use $_POST['postcode']

If you add the variable to source:

$(document).ready(function () {
    $('.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;
        }
    });
});

Then you would use $_GET['postcode']

Mr. Meeseeks
  • 1,841
  • 2
  • 21
  • 37
  • Yes, all works. Just one moment I can not understand. in external php there is '$to_execute = $_REQUEST['term']. '%';' But what is `term`? I can not find where `term` is defined – Andris Dec 08 '13 at 16:16
  • The `$_REQUEST` variable holds both `$_GET` and `$_POST` (as well as `$_COOKIE`) variables. `$_REQUEST['term']` would be trying to get a variable called `term` that was passed with `$_GET` or `$_POST`. – Mr. Meeseeks Dec 08 '13 at 16:20
  • `REQUEST` I understand, but can not understand why `term`. There is no input `name` or `id` with word/name `term` – Andris Dec 08 '13 at 16:25
  • `term` is added from the jQuery autocomplete widget. `"For example, if the source option is set to "http://example.com" and the user types foo, a GET request would be made to http://example.com?term=foo. The data itself can be in the same format as the local data described above."` - From [http://api.jqueryui.com/autocomplete/#option-source](http://api.jqueryui.com/autocomplete/#option-source) – Mr. Meeseeks Dec 08 '13 at 16:31