MechaMetalHead MechaMetalHead -4 years ago 113
HTML Question

php - enable elements if not empty when retrieve value from database

Here is the whole code i'm working on.

<?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']; ?>" />
<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;
} 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_type',`client_details` = '$client_details' WHERE `id` = '$id'";
if ($con->query($query) === TRUE) {
echo "<br><br> Updated successfully <br>";
echo $query;
} else {
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
}
?>


But I want the elements enabled automatically when the user go to the edit form without having to click the radio button again just to enable it again.

Something like for example this line of code.

<input type="radio" name="client_type" id = "client1" value="Division" <?php echo ($client_type == 'Division')? "checked" : "" ?> <?php if($client_type == 'External'){ echo 'disabled="disabled"';} ?> onclick="toggleDivision()"/> Division


When I try to run this line of code, it still works the same nothing different.

Are there any alternatives or examples to have the elements enabled when values are retrieved from the database?

Answer Source

You can do it using ajax. Ajax will make it live. Use ajax with php to make it live. Read more about it here : https://www.w3schools.com/php/php_ajax_php.asp

Ajax is a javascript library like query.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download