我正在建立一个网络,它将自由职业者分组在一起,并允许潜在客户浏览包含其详细信息的个人资料。
在我的数据库中,我存储了用户将使用下拉列表或单选/复选框字段通过表单输入的一些值。通过编辑页面,他们可以修改该数据。
我正在努力使如何将这些字段预先填充(如果该值存在于数据库中)(可能是在创建其概要文件时)使用它们已经获得的值进行填充。我已经设法通过将列值作为表单字段值进行回显来对常规文本/输入字段进行处理,但无法弄清楚如何使用其他字段来实现它。
更新:我需要从数据库中提取值,并让表单字段将其显示为预选/默认条目。
如果我将其保留为空白,则意味着用户将不加覆盖任何现有数据,并擦除之前为该字段输入的所有内容。
下面是一个示例下拉字段;
<div class="item-content">
<div>Experience</div>
<select class="form-control" name="profile_experience" id="profile_experience">
<option value="1">Amateur</option>
<option value="2">Semi Professional</option>
<option value="3">Professional</option>
</select>
</div>
我正在使用以下内容获取值;
<?php
$id=$_SESSION['user']['id'];
$result = $db->prepare("SELECT * FROM profiles WHERE user_id= :userid");
$result->bindParam(':userid', $id);
$result->execute();
for($i=0; $currentprofile = $result->fetch(); $i++){
?>
<!--FORM HERE-->
<?php
}
?>
检索并存储存储的值
<?php
$query = "SELECT id FROM Tablename WHERE YOUR_CONDITION";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
$selectedOption = $row['id'];
}
else
{
$selectedOption = ''; // Your default selection of $cc
}
$profile_experience_array = array(1=>'Amateur',
2=>'Semi Professional',
3=>'Professional');
?>
以下代码显示profile_experience_array的所有选项。$ key将检查数据库值($ selectedOption),默认情况下将选中该文本。
<div class="item-content">
<div>Experience</div>
<select class="form-control" name="profile_experience" id="profile_experience">
<option value="0">Select</option>
<?php
foreach ($profile_experience_array as $key => $text)
{
if ($key == $selectedOption)
{
echo '<option value="'.$key.'" selected="selected">'.trim($text).'</option>';
}
else
{
echo '<option value="'.$key.'">'.trim($text).'</option>';
}
}
?>
</select>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句