当我从下拉列表中选择年份之一时,如何在下面的表格的输入字段中显示与年份相关的标题。
$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>
答案 0 :(得分:0)
以下是html格式所需的一些更改。是否需要知道是否要使用JavaScript或php进行此更改?如果您不知道它们之间的区别,那就是php是服务器端,它将仅在页面加载时进行解析,而javascript可以做到这一点,而无需刷新页面或重新加载页面。
$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="<?php $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="<?php $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>