如何使用下拉列表从数据库中获取数据,并在php mysql中显示到输入字段中?
当我从下拉列表中选择一个年份时,如何在下表的输入字段中显示与年份相关的标题如何使用下拉列表从数据库中获取数据,并在php mysql中显示到输入字段中?,php,mysql,forms,Php,Mysql,Forms,当我从下拉列表中选择一个年份时,如何在下表的输入字段中显示与年份相关的标题 $formationSQL = "SELECT title FROM formationacademique"; $result = $connection->query($formationSQL); <form method="post"> <label>Select year:</label> <selec
$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>
$formationSQL=“从formationacademique中选择标题”;
$result=$connection->query($formationSQL);
选择年份:
标题:
下面是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>
$formationSQL=“从formationacademique中选择标题”;
$result=$connection->query($formationSQL);
选择年份:
标题:
您正在寻找php加载值或页面上发生变化的javascript吗?@RudyLister only php将年份记录从mysql数据库加载到下拉列表中,然后当我从下拉列表中选择一个年份时,与年份相关的标题必须显示到标题输入字段中。@RudyLister我的下拉列表工作正常,只想显示标题输入到与年份相关的输入字段中。我不确定您的数据是否有标题值,或者您是否使用ID_格式作为标题。
<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>