我想要使用jQuery基于从外部PHP文件使用json_encode(数组)发送的数组来填充文本框。选择的选择框选项决定将哪个选项值发送到外部PHP文件。不幸的是,我的文本框始终是空的。我一直在试图用Firebug和lint网站进行调试,但我无法弄清楚为什么没有人口。 忽视这个安全员,请
主索引页的代码片段:
function loadDoc()
{
$('#verb').live('change', function()
{
$.post("loadTextBox.php", {verb_value: $(this).val()},
function(data)
{
$("#textbox").html(data.first);
$("#textbox2").html(data.second);
$("#textbox3").html(data.third);
$("#textbox4").html(data.fourth);
$("#textbox5").html(data.fifth);
$("#textbox6").html(data.sitxh);
},"json");
});
}
外部PHP文件代码:
<?php
header('Content-Type:application/json;charset=utf-8');
$file_absolute = "---placeholder for correct file path---";
include_once($file_absolute);
$mysql = new mysqli($db_host, $db_username, $db_password, $db_name);
$verb_value = $_POST['verb_value'];
$mysql->query("SET CHARACTER SET 'utf8'");
$result = $mysql->query("SELECT present_tense FROM $verb_value");
$queryResult = array();
while ($row = $result->fetch_object())
{
$queryResult[] = $row->present_tense;
}
$textboxValue = $queryResult[0];
$textboxValue2 = $queryResult[1];
$textboxValue3 = $queryResult[2];
$textboxValue4 = $queryResult[3];
$textboxValue5 = $queryResult[4];
$textboxValue6 = $queryResult[5];
$array = array('first'=>$textboxValue,'second'=>
$textboxValue2,'third'=>$textboxValue3,'fourth'=>
$textboxValue4,'fifth'=>$textboxValue5,'sixth'=>
$textboxValue6);
echo json_encode($array);
?>
我通过删除.live()方法并使用.val()而不是.html()修改了代码。文本框仍然没有填充。这是我的代码:http://pastebin.com/UJf57E6J – programm3r
@ programm3r似乎很好。最后是'#mySelectBox'还是'#verb'?你从服务器得到什么(数据中有什么)? – kapa
它应该是#verb(选择框的ID)。我编辑了上面的代码。仍然没有人满足。你如何看待数据中的内容?你可以在Firebug中看到这个,如果是,在哪里?也许有办法将什么数据记录到控制台? – programm3r