gpt4 book ai didi

PHP - 复选框值以编辑形式显示在文本框中

转载 作者:行者123 更新时间:2023-11-28 00:59:30 24 4
gpt4 key购买 nike

我在一堆复选框和一个文本框上制作了一个编辑表单。这是我的代码

<?php
require("config.php");
$id = $_GET['id'];

$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
while ($row = $result->fetch_assoc())
{
$subunit = explode(',',$row['subunit']);
?>

<form action="" method="GET">
ID: <?php echo $id; ?><br>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Sub-Category Unit:
<input type="checkbox" name="subunit[]" value="OFL" <?php in_array('OFL', $subunit) ? print "checked" : ""; ?>>OFL
<input type="checkbox" name="subunit[]" value="HVTL" <?php in_array('HVTL', $subunit) ? print "checked" : ""; ?>>HVTL
<input type="checkbox" name="subunit[]" value="PINS" <?php in_array('PINS', $subunit) ? print "checked" : ""; ?>>PINS
<input type="checkbox" name="subunit[]" value="FEG" <?php in_array('FEG', $subunit) ? print "checked" : ""; ?>>FEG
<input type="checkbox" name="subunit[]" value="PC" <?php in_array('PC', $subunit) ? print "checked" : ""; ?>>PC
<input type="checkbox" name="subunit[]" value="PI" <?php in_array('PI', $subunit) ? print "checked" : ""; ?>>PI<br> &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp

Others:
<input type="text" name="subunit[]" value="<?php echo $row['subunit']; ?>"/>
<br><br>

<input type="submit" name="edit" value="Update" />
</form>

<?php
}
if(isset($_GET['edit']) ){
$subunit = isset($_GET ['subunit']) ? implode(",", $_GET['subunit']) :null;

$upd= "UPDATE `contracts` SET `subunit` = '$subunit' WHERE `id` = '$id'";

if($do_upd = $con->query($upd)) {
echo "Update Success<br>";
echo "$upd <br>";
echo "$do_upd <br>";
echo "<a href='viewcheck.php?id=$id'>View</a>";
} else {
echo "Update Fail<br>";
echo "$upd <br>";
echo "$do_upd <br>";
echo "<a href='viewcheck.php?id=$id'>View</a>";
}
}
?>

代码运行良好。当我更新数据后再次回到编辑表单时,它看起来像这样。

capture1

我不希望所有的复选框值都显示在文本框中。但是我想对复选框和文本框使用相同的属性。我该怎么做?

capture2

最佳答案

$subunit = explode(',',$row['subunit']); 行下方的 while 循环内,需要放置以下代码:

$array_all=array('OFL','HVTL','PINS','FEG','PC','PI');
$a=array_values(array_diff($subunit,$array_all));

$a 将为您提供 Others 文本字段的值。您需要将 Others 的值替换为:

<input type="text" name="subunit[]" value="<?php echo $a[0]; ?>"/>

关于PHP - 复选框值以编辑形式显示在文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42800466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com