jQuery需要将额外的值传递给外部php(结果得不到外部php的响应)

时间:2013-12-08 16:05:04

标签: jquery autocomplete

使用此处的示例http://www.htmlblog.us/jquery-autocomplete

我修改了示例并获得了这样的工作代码

$(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>

使用此代码,如果我在<input id="zipsearch" type="text" class="zipsearch" />中输入内容,则输入的值将传递给外部php,处理并返回。到目前为止还好。

但是需要从<input type="hidden" id="additional_value" value="1"/>

向外部php发送值

找到示例How do I pass an extra parameter to Jquery Autocomplete field?

试图修改并获得

$('.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;
    }
});

但外部php没有回应

请提出建议需要纠正的内容?

再次尝试修改并获取

$("#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;
}
});

这部分有效。但是如何在外部php中访问additional_value?试图例如'account_description' => $_POST['additional_value']变空(空)

外部php就像这样

$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) ){

1 个答案:

答案 0 :(得分:1)

在您的修改中,您"suggest_zip.php?postcode=" + $('#additional_value').val()在您的PHP代码中使用$_GET['postcode'](因为您将$_GET变量设置为postcode

如果您向自动填充添加了data

$(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;
        }
    });
});

然后你会使用$_POST['postcode']

如果您将变量添加到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;
        }
    });
});

然后你会使用$_GET['postcode']