I am trying to create an edit button in table rows, and on click of it the data in the form gets populated.
This is the edit button:
$tableHTML .= "<td><a class='btn btn-primary btn-lg' onclick=history.back() href='index.php?id=" . $row['ip_address'] . "&edit=y&app_server=" . $row['app_server'] . "'>Edit</a></td>";
Main page where changes are being implied:
<?php
include "conn.php";
$cucId = '';
$ipaddress = '';
$os_val = '';
$cpu_val = '';
$ram_val = '';
$storage_val = '';
//$ip = $_REQUEST['id'];
$ip = isset($_POST['id']) ? $_POST['id'] : '';
if (isset($_GET['edit']) && $_GET['edit'] == 'y') {
// Code to execute if 'edit' is present and has the value 'y'
} else {
// Code to execute if 'edit' is not present or doesn't have the value 'y'
}
if (isset($_GET['edit']) && $_GET['edit'] == 'y' && $ip != '') {
$sql = "SELECT * FROM info_table WHERE ip_address='$ip'";
$result = $link->query($sql);
while ($row = $result->fetch_assoc()) {
$cucId = $row['cuc_id'];
$ipaddress = $row['ip_address'];
$os_val = $row['os_val'];
$cpu_val = $row['cpu_val'];
$ram_val = $row['ram_val'];
$storage_val = $row['storage_val'];
}
echo $cucId;
echo $ipaddress;
}
echo $os_val;
?>
<div id="cuc-choose">
<center>
<label for="cuc-name">Choose cuc server:</label>
<select name="cuc-name" id="cuc">
<option selected>SELECT </option>
<?php include "conn.php";
$sql = "SELECT id, cuc_name FROM cuc_namem";
$result = $link->query($sql);
while ($row = $result->fetch_assoc()) {
if ($_GET['edit'] == 'y') {
$selected = ($row['id'] == $cucId) ? 'selected' : '';
echo "<option value='" . $row['id'] . "' $selected>" . $row['cuc_name'] . "</option>";
}
if ($_GET['edit'] != 'y') {
echo "<option value='" . $row['id'] . "'>" . $row['cuc_name'] . "</option>";
}
}
?>
The form is similar to this cuc choose. when I click edit, the form data should populate (it contains text fields and dropdowns).