首页 > 解决方案 > 如何使用选择选项将 id 提取到数据库

问题描述

确实,我的问题很简单。当我更改“选项”时,我必须拉它的 ID。也许它可以工作javascript。但我不知道javascript。

<h4><u> Upload Image </u></h4>
<form action="a.php" method="post" enctype="multipart/form-data">

<label style="font-size:16px;" for="selectalbum">Select Album</label>
<select style="margin-left:10px;" name="selectalbum">

<?php
$imagequery=$db->prepare("SELECT * FROM imagealbum");
$imagequery->execute(array());
$imgquery=$imagequery->fetchAll(PDO::FETCH_ASSOC);
foreach($imgquery as $imagealbum) {
?>

<option value="<?php echo $imagealbum['imagealbum_id']; ?>"><?php echo $imagealbum['imagealbum_name']; ?></option>

<?php } ?>
</select>
<br>
<input type="text" name="aaa" style="text-align:center;">
<br><br>

<input type="text" style="text-align:center;" value="<?php echo $imagealbum['imagealbum_id']; ?>">

<br><br>
<input type="file" name="files[]" value="Upload Image" />
<br>
<button style="margin-top:7px; width:90px; height:30px; background-color:green; color:#fff; border-radius:7px;" name="submit">Submit</button>
</form>

标签: phphtml

解决方案


尝试使用 getId 的函数添加 onchange 事件,如下所示:

const getId = () => {
  const album = document.querySelector('#albums');
  const log = document.querySelector('#log');
  log.innerHTML = ` Id: ${album.value}`;
}

window.onload = function() {
  getId();
};
<label for="albums">Select Album:</label>
<select name="albums" id="albums" onchange="getId()">
  <option value="123">Album 1</option>
  <option value="321">Album 2</option>
  <option value="159">Album 3</option>
</select>


<div id="log">
</div>


推荐阅读