当我从下拉列表中选择年份之一时,如何在下面表单的输入字段中显示与年份相关的标题。
$formationSQL = "SELECT title FROM formationacademique";
$result = $connection->query($formationSQL);
<form method="post">
<label>Select year:</label>
<select>
<?php foreach($result as $formation): ?>
<option id="formationID" name="formationID" value="<?= $formation['ID_Formation']; ?>"><?= $formation['year']; ?></option>
<?php endforeach; ?>
</select>
<label>title:</label>
<input type="text value="">
</form>
以下是 html 格式需要进行的一些更改。需要知道您是否希望在 javascript 或 php 中进行此更改?如果您不知道区别,那就是 php 是服务器端,只会在页面加载时进行解析,而 javascript 可以在不刷新或重新加载页面的情况下完成此操作。
<?php
$formationSQL = "SELECT title FROM formationacademique";
$result = $connection->query($formationSQL);
?>
<form method="post">
<label for="formationID">Select year:</label>
<select id="formationID" name="formationID">
<?php foreach($result as $formation): ?>
<option value="<?= $formation['ID_Formation']; ?>">
<?= $formation['year']; ?>
</option>
<?php endforeach; ?>
</select>
<label for="input_title">title:</label>
<input type="text" id="input_title" name="input_title" value="<?=$formation['ID_Formation']; ?>">
</form>
将以下脚本内容添加到您的 js 文件中,或在上面的表单后面添加脚本,以更改输入值的 javascript 语句。
<script>
var year_select = document.getElementById( 'formationID' );
var year_title = document.getElementById( 'input_title' );
year_select.addEventListener( 'change', function( e ) {
year_title.value = year_select.selectedIndex.value;
});
</script>