使用表单模式遇到麻烦。基本上它是第一次工作,但每隔一段时间它就会保留第一次的数据。
基本上我有一个页面(metadata.php),它有一个按钮来打开模态并根据一些表数据传递一个ID。
由于某种原因,$ ID变量卡住了,当用户关闭模态并单击另一个编辑按钮时,它会显示第一个对象的表单。我很确定这是因为我没有清除PHP变量更正,但每次都应该通过$ id = $ _REQUEST ['id']来获取$ ID。
任何帮助将不胜感激。
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)。请注意,正文是空白的,因为我用它来打开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表单(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>
答案 0 :(得分:0)
添加了以下脚本,它似乎正在运行。虽然它没有清除PHP变量,但是当打开一个新模态时它似乎正在刷新它们。
<script>
$('body').on('hidden.bs.modal', '.modal', function () {
$(this).removeData('bs.modal');
});
</script>