您似乎混淆了服务器端和客户端操作。
PDO 在服务器端运行并与数据库服务器通信。
JavaScript(在本例中为 jQuery)在客户端运行并在 DOM 上运行。
AJAX 是这两者之间的某种联系。
因此,如果您想使用数据库中的一些值填充输入字段,您可以在服务器端进行:
<?php
//run query, fetch array and store it in $sport
?>
<input type="text" name="full_name" value="<?php echo $sport['full-name']; ?>" />
<input type="text" name="short_name" value="<?php echo $sport['short-name']; ?>" />
<input type="text" name="abbreviation" value="<?php echo $sport['abb-name']; ?>" />
或者您可以在客户端执行此操作(例如,如果用户单击链接/按钮/其他):
<?php
//this is the script you make an AJAX-request to, e.g. get_data.php
//run query, fetch array and store it in $sport
echo json_encode($sport); //echo the array in JSON-format
这是提供给用户的页面:
...
<input type="text" name="full_name" id="input-full-name" value="" />
<input type="text" name="short_name" id="input-short-name" value="" />
<input type="text" name="abbreviation" id="input-abb-name" value="" />
...
<button id="populate_btn">Populate Data</button>
<scrip>
$('#populate_btn').click(functiion(){
$.post('get_data.php', {}, function(sport) {
//sport now contains the json-array
$.each(sport, function(key, value) {
$('#input-'+key).val(value);
});
});
});
</script>
这是一个非常基本的例子,但我希望你能明白。另请注意,给定的 AJAX 示例仅在数组的键与某种输入字段的id-attributes 匹配时才有效,例如:$sport['full-name'] and <input id="input-full-name" />。这使得使用它变得更加容易。