1
我是一名PHP初學者,今天我開始學習Mootools。我正在試驗一個自動完成搜索框,在您提交表單後發送一個數組。您可以鍵入多個名稱並搜索所有這些名稱。在一頁上發送並打印PHP Array結果?
我試圖做的是採取數組中的mootools,將其發送到PHP和不離開頁面顯示的結果。
// Autocomplete initialization
var t4 = new TextboxList('form_tags_input_3', {unique: true, plugins: {autocomplete: {}}});
// sample data loading with json, but can be jsonp, local, etc.
// the only requirement is that you call setValues with an array of this format:
// [
// [id, bit_plaintext (on which search is performed), bit_html (optional, otherwise plain_text is used), autocomplete_html (html for the item displayed in the autocomplete suggestions dropdown)]
// ]
// read autocomplete.php for a JSON response example
t4.container.addClass('textboxlist-loading');
new Request.JSON({url: 'autocomplete.php', onSuccess: function(r) {
t4.plugins['autocomplete'].setValues(r);
t4.container.removeClass('textboxlist-loading');
}}).send();
這裏的autocomplete.php
$response = array();
$names = array('Some name', 'Some name 2', 'etc');
// make sure they're sorted alphabetically, for binary search tests
sort($names);
foreach ($names as $i => $name)
{
$filename = str_replace(' ', '', strtolower($name));
$response[] = array($i, $name, null, '<img src="images/'. $filename . (file_exists('images/' . $filename . '.jpg') ? '.jpg' : '.png') .'" /> ' . $name);
}
header('Content-type: application/json');
echo json_encode($response);
Submit.php
<?php
print_r($_POST);
?>
我的PHP:
<?php
$result['msg'] = print_r($_POST);
echo json_encode($result);
?>
這裏是我在index2.php文件所做的更改:
<input type="submit" name="submitform" value="Submit" id="submitform" />
<p id="response"></p>
$('#submitform').addEvent('click', function(){
new Request.JSON({
url: "inc/php/json.php",
onSuccess: function(response){
$('response').set('html',+data.result);
}
}).get($('findnames'));
});
當你按下提交,沒有任何反應,所以很明顯我的地方犯了一個錯誤,我似乎無法弄清楚。
嗨Olli,謝謝你的回答:)。這是你的意思嗎? <---HTML----> <--- MooTools的----> $( '調用getResult')。的addEvent( '點擊',函數(){ <---PHP----> <?php print_r($ _ POST); ?> – user944589
我更新了頁面,但是我認爲我在某處確認了一個錯誤,因爲它沒有得到結果 – user944589
我認爲這是問題,但我不知道該怎麼做取而代之的是:$('response').set('html',$ _ POST); – user944589