dqbhdsec59405 2014-12-09 23:50
浏览 39
已采纳

模态关闭后取消设置PHP变量

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">&times;</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>
  • 写回答

1条回答 默认 最新

  • duanduo3712 2014-12-11 15:35
    关注

    Added the following script and it seems to be working. Although it doesn't clear the PHP variables it seems to be refreshing them when a new modal is opened.

    <script>
    $('body').on('hidden.bs.modal', '.modal', function () {
    $(this).removeData('bs.modal');
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?