Having troubles with a form modal. Basically it works the first time but then every other time it keeps the data from the first.
Basically I have an page (metadata.php) that has a button to open the modal and pass an ID based on some table data.
For some reason the $ID variable is getting stuck and when the user closes the modal and clicks on another edit button it shows the form for the first object. I'm pretty sure it's because i'm not clearing the PHP variables corrects, but the $ID should at least be grabbed every time via the $id = $_REQUEST['id'];.
Any help would be appreciated.
HTML (metadata.php):
<a data-toggle="modal" data-target="#updateModal" class="btn-sm btn-success" href="./update.php?id='.$row['CLUSTERNAME'].'">Edit</a>
HTML (metadata.php). Note the body is blank because I use it to open update.php:
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog"
aria-labelledby="updateModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
</div>
</div>
</div>
</div>
PHP (update.php):
<?php
include("../scripts/connect.php");
include("../config.php");
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( null==$id ) {
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
if ( !empty($_POST)) {
// keep track validation errors
$datacenterError = null;
$tierError = null;
// keep track post values
$datacenter = $_POST['datacenter'];
$tier = $_POST['tier'];
// validate input
$valid = true;
$note = preg_replace('/[^\w%!&, ]/', '', $note); // Removes special chars.
if (empty($datacenter)) {
$datacenterError = 'Please select a Datacenter';
$valid = false;
}
if (empty($tier)) {
$tierError = 'Please select a Tier';
$valid = false;
}
// update data
if ($valid) {
$sql = "UPDATE <table> SET DATACENTER='$datacenter', TIER='$tier' WHERE CLUSTERNAME = '$id'";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
} else {
$sql = "select CLUSTERNAME,VCENTER_NAME,DATACENTER,TIER FROM <table> WHERE CLUSTERNAME = '$id'";
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
$clustername = $row['CLUSTERNAME'];
$vcentername = $row['VCENTER_NAME'];
$datacenter = $row['DATACENTER'];
$tier = $row['TIER'];
}
}
?>
HTML form (update.php):
<body>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Update a Cluster</h4>
</div>
<div class="modal-body">
<div class="container">
<!--Default buttons with dropdown menu-->
<form class="form-horizontal" action="metadata_update.php?id=<?php echo $id?>" method="post">
<div class="col-lg-6">
<div class="form-group">
<label class="col-md-4 control-label" for="clustername">Cluster</label>
<div class="col-md-6">
<input id="clustername" name="clustername" type="text" value="<?php echo !empty($clustername)?$clustername:'';?>" class="form-control input-md" disabled>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="vcentername">vCenter</label>
<div class="col-md-6">
<input id="vcentername" name="vcentername" type="text" value="<?php echo !empty($vcentername)?$vcentername:'';?>" class="form-control input-md" disabled>
</div>
</div>
<div class="<?php echo !empty($datacenterError)?'error':'';?> form-group">
<label class="col-md-4 control-label" for="datacenter">Datacenter</label>
<div class="col-md-6">
<select id="datacenter" name="datacenter" class="form-control" value="<?php echo !empty($datacenter)?$datacenter:'';?>>
<option value="test">test</option>
<option value="test2">test2</option>
</select>
<?php if (!empty($datacenterError)): ?>
<span class="help-block"><?php echo $datacenterError;?></span>
<?php endif;?>
</div>
</div>
<div class="<?php echo !empty($tierError)?'error':'';?> form-group">
<label class="col-md-4 control-label" for="tier">Tier</label>
<div class="col-md-6">
<select id="tier" name="tier" class="form-control">
<option value="All">All</option>
<option value="Empty">Empty</option>
</select>
<?php if (!empty($tierError)): ?>
<span class="help-block"><?php echo $tierError;?></span>
<?php endif;?>
</div>
</div>
<div class="modal-footer">
<div class="form-actions">
<button type="submit" class="btn btn-success">Update</button>
<button type="button" data-dismiss="modal" class="btn btn-default">Back</button>
</div>
</form>
</div>
</div> <!-- /container -->
</div> <!-- /col -->
</div>