PHP - 在不同的文本框

时间:2017-03-27 02:20:29

标签: php mysql mysqli

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

Division->(下拉列表和其他:文本框)和External->(文本框)属于同一个" client_details"。

This is the example of the edit form.

这是我制作的代码。

<?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();                                 
}
?>
编辑:我的意思是这个。

正如您可以看到链接中的示例图片,&#34; Generation&#34;更新数据后,值显示在分区和外部下的所有文本框中,因为该值来自下拉列表。

&#34;其他&#34;文本框正在划分,但只有在选择值&#34;其他&#34;之后才能输入。从分部下拉列表中。

我只希望文本框中显示的分区值仅在分区下,而不是在选择分区单选按钮时在外部下方。如果我在文本框中选择外部和输入数据,我只希望在更新后仅在外部的文本框中显示该值。

当我选择值而不是&#34;其他&#34;值,该值不应显示在Division下的文本框中。当我选择&#34;其他&#34;值,然后该值应显示在&#39;其他&#39;文本框但我不希望该值也显示在外部的文本框中。

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

1 个答案:

答案 0 :(得分:0)

嗯,难怪为什么两者都显示相同的东西,它们的值都只是<?php echo $row['client_details']; ?>

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

E.g。

<?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 />