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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度