首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP -在两个不同的文本框上显示相同的值

PHP -在两个不同的文本框上显示相同的值
EN

Stack Overflow用户
提问于 2017-03-27 02:20:28
回答 1查看 53关注 0票数 0

所以,我做了一个代码来编辑单选按钮,下拉列表和文本框。

分区->(下拉列表和其他: textbox)和外部->(Textbox)位于相同的"client_details“下。

这是编辑表单的示例。

这是我做的密码。

代码语言:javascript
复制
<?php
require("config.php");
$id = filter_input(INPUT_GET, 'id');
?>

<html>
<head>
<title> Edit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    
    ID: <input type="hidden" name="id" value="<?php echo $id; ?>" />
        <?php
            $sql = "SELECT * FROM contracts WHERE `id` = $id";
            $result = $con->query($sql);
            $row = $result->fetch_assoc();
            $client_type = $row['client_type'];
        ?>

    <label for = "client1">
    <input type="radio" name="client_type" id = "client1" value="Division" <?php echo ($client_type == 'Division')? "checked" : "" ?> onclick="toggleDivision()"/> Division
    </label>
    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 
    <label for ="client2">
    <input type="radio" name="client_type" id = "client2" value="External" <?php echo ($client_type == 'External')? "checked" : "" ?> onclick="toggleExternal()"/> External
    </label>
    &nbsp 
    <input type="text" id="extText" name="client_details2" value="<?php echo $row['client_details']; ?>" disabled /> 
    <br><br>

    <div id="division">
        Division:
        <select  id="mySelect" name="client_details" onclick="enableTextbox()" disabled>
            <option value="Choose" <?php echo $row['client_details'] == 'Choose' ? "selected" : ""; ?> />Choose Division...</option>
            <option value="Distribution" <?php echo $row['client_details'] == 'Distribution' ? "selected" : ""; ?> />Distribution</option>
            <option value="Transmission" <?php echo $row['client_details'] == 'Transmission' ? "selected" : ""; ?> />Transmission</option>
            <option value="Generation" <?php echo $row['client_details'] == 'Generation' ? "selected" : ""; ?> />Generation</option>
            <option value="Procument" <?php echo $row['client_details'] == 'Procument' ? "selected" : ""; ?> />Procument</option>
            <option value="Other" <?php echo $row['client_details'] == 'Other' ? "selected" : ""; ?> />Others</option>
        </select>   
        <br><br>
        Others:<input type="text" id="otherTxt" name="client_details1" value="<?php echo $row['client_details']; ?>"  disabled />
        <br>
        </div>
        <br>
        <input type="submit" name="submit" value="Submit"/>
</form>     

<script type="text/javascript">

function toggleExternal() {
    document.getElementById("extText").disabled = false;

    var divis_el = document.getElementById("division");
    for (var i = 0; i < divis_el.children.length; i++) {
        divis_el.children[i].disabled = true;
    }
}
function toggleDivision() {
    document.getElementById("extText").disabled = true;
    var val = document.getElementById("mySelect").selectedIndex;
    var divis_el = document.getElementById("division");
    for (var i = 0; i < divis_el.children.length; i++) {
        divis_el.children[i].disabled = false;
        divis_el.children[5].disabled = true;
    }
}

function enableTextbox() {
var val = document.getElementById("mySelect").selectedIndex;
if (val == 0 || val == 1 ||val == 2 ||val == 3 ||val == 4) { document.getElementById("otherTxt").disabled = true}
if (val == 5) { document.getElementById("otherTxt").disabled = false; }
}
</script>   

</body>

<?php
if(isset($_POST['submit'])) {

$client_type = isset($_POST ['client_type']) ? $_POST['client_type'] :null;
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;

if($client_type == 'Division'){
$client_details = isset($_POST ['client_details1']) ? $_POST['client_details1'] :null;
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null; // both variables under if statement are needed to successfully update the data avoiding blank values.
} else {
$client_details = isset($_POST ['client_details2']) ? $_POST['client_details2'] :null;
}

if($client_details == 'Other') {
    $client_details = isset($_POST ['client_details1']) ? $_POST['client_details1'] :null;
} 
$query = "UPDATE contracts set `client_type`=?, `client_details`=? WHERE `id`= ?";

$stmt = $con->prepare($query);

$stmt->bind_param("ssi", $client_type, $client_details, $id);
$stmt->execute();


if ($stmt->errno){
     echo "FAILURE!!! " . $stmt->error;
} else {
    echo "<br>Updated";
}

$stmt->close();
$con->close();                                 
}
?>

编辑:我的意思是这个。

正如您从链接中看到的示例图片一样,“生成”值在我更新数据后显示在“分区”和“外部”的所有文本框中,因为该值是从下拉列表中提取的。

“其他”文本框在除法中,但只有在从“除法”下的下拉列表中选择“其他”值之后才能输入。

我只希望只在textbox中显示分区值,而不是选择“分区”单选按钮时显示在“外部”下。如果我在textbox中选择了外部数据和输入数据,我只希望在更新后在textbox中显示在External下的值。

当我选择值而不是“其他”值时,不应在“除”下的文本框中显示该值。当我选择“其他”值时,该值应该显示在“其他”文本框中,但我不希望该值也显示在外部文本框中。

我想问的问题是如何使文本框值分别显示值。我不希望在所有下拉列表和文本框中显示相同的值?我该怎么做?对不起,英语是我的第二语言。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-27 03:21:40

难怪两者都在显示samething,它们的值都是<?php echo $row['client_details']; ?>

您已经有了一个检查变量,只是不确定为什么不在这里使用它。

例如。

代码语言:javascript
复制
<?php $extText = ($client_type == 'External') ? $row['client_details'] : "" ?>
<?php $divText = ($client_type == 'Division') ? $row['client_details'] : "" ?>
<input type="text" id="extText" name="client_details2" value="<?php echo extText; ?>" disabled /> 
Others:<input type="text" id="otherTxt" name="client_details1" value="<?php echo $divText; ?>"  disabled />
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43036974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档