两个Bootstrap Modals用于编辑两个不同的文件内容

时间:2014-07-01 03:40:50

标签: php if-statement twitter-bootstrap-3

首先让我先说我知道这将是一个非常基本的问题。我是PHP的新手,并且在这项任务中苦苦挣扎。

说明: 我正在本地WAMP服务器上制作一个小应用程序来管理基本的客户端数据库,并管理我的所有VHOST项目以进行开发。我知道编辑Windows主机文件存在安全问题,因为我要请求,但这将严格地说是本地站点。

我有以下PHP

<?php include 'template-parts/header.php' /** calling of header(to make it uniform in all template file) **/?>  

<div class="container home">
    <h3> Delete </h3>



    <div class="btn-group">
            <button data-toggle="modal" class="btn btn-primary btn-sm" name="hostsedit" data-target="#modalhost"><span class="glyphicon glyphicon-user"></span> Edit Windows Host File</button>
            <button data-toggle="modal" class="btn btn-primary btn-sm" name="vhostsedit" data-target="#modalvhost"><span class="glyphicon glyphicon-trash"></span> Edit VHOST.conf</button>
    </div>


    <?php

        // configuration
        $url = 'delete.php';
        $file = 'C:/Windows/System32/Drivers/etc/hosts';

        // check if form has been submitted
        if (isset($_POST['text']))
        {
        // save the text contents
        file_put_contents($file, $_POST['text']);

        // redirect to form again
        header(sprintf('Location: %s', $url));
        printf('<a href="%s">Moved</a>.', htmlspecialchars($url));
        exit();
    }

    // read the textfile
    $text = file_get_contents($file);   
    ?>


    <!-- Modal 1 -->
    <div class="modal fade" id="modalhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Delete Host File Entry</h4>
            </div><!-- /modal-header -->
            <div class="modal-body">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-xs-12 col-md-12">
                            <!-- HTML form -->
                            <form action="" method="post">
                                <textarea name="text" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br />
                                <p><strong>NOTE:</strong> Ensure only lines similar to <kbd>127.0.0.1 www.dev.xxxxx</kbd> are deleted</p>
                        </div>
                    </div>
                </div>                        
            </div><!-- /modal-body -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Save changes</button>
            </div> 
            </form>
        </div> <!-- /.modal-content -->
    </div> <!-- /.modal-dialog -->
</div> <!-- /.modal -->


<!-- Modal 2 --> 
<div class="modal fade" id="modalvhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Delete VHOST File Entry</h4>
            </div><!-- /modal-header -->
            <div class="modal-body">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-xs-12 col-md-12">
                            <!-- HTML form -->
                            <form action="" method="post">
                                <textarea name="text" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br />
                        </div>
                    </div>
                </div>                        
            </div><!-- /modal-body -->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Save changes</button>
            </div> 
            </form>
        </div> <!-- /.modal-content -->
    </div> <!-- /.modal-dialog -->
</div> <!-- /.modal -->
</div>  
</div>
</body>
</html>

第一个模态完美运行,我单击按钮<button data-toggle="modal" class="btn btn-primary btn-sm" name="hostsedit" data-target="#modalhost"><span class="glyphicon glyphicon-user"></span> Edit Windows Host File</button>这将打开Bootstrap模式,在textarea中显示Windows主机文件并允许我随意添加和删除,我点击模式&#39 ; s保存按钮,它可以工作。

问题我需要使用第二个模态窗口实现相同的操作,但是这需要编辑C:/wamp/bin/apache/apache2.4.9/conf/extra/httpd-vhosts.conf'

我根本不知道如何复制第一个正常运行的PHP,现在也可以使用VHOST编辑要求。

非常感谢任何帮助或指示。

修改

嗨,Neal,我尝试将你的解决方案放到位并遇到一些错误,我不能完全确定如何过去......

我目前在我的PHP文件中有这个:

<div class="container home">
<h3> Delete </h3>



<div class="btn-group">
        <button data-toggle="modal" class="btn btn-primary btn-sm" name="hostsedit" data-target="#modalhost"><span class="glyphicon glyphicon-user"></span> Edit Windows Host File</button>
        <button data-toggle="modal" class="btn btn-primary btn-sm" name="vhostsedit" data-target="#modalvhost"><span class="glyphicon glyphicon-trash"></span> Edit VHOST.conf</button>
</div>


<?php
// check if form has been submitted
if (!empty($_POST['hostinput'])){ //i prefer to use empty rather than isset you can read about it

    // configuration
    $url = 'delete.php';
    $file = 'C:/Windows/System32/Drivers/etc/hosts';

    // check if form has been submitted
    if (isset($_POST['hostinput']))
    {
    // save the text contents
    file_put_contents($file, $_POST['hostinput']);

    // redirect to form again
    header(sprintf('Location: %s', $url));
    printf('<a href="%s">Moved</a>.', htmlspecialchars($url));
    exit();
    }
    // read the textfile
    $text = file_get_contents($file);  
}
else if(!empty($_POST['vhostinput'])){
    // configuration
    $url = 'delete.php';
    $file = 'C:/wamp/bin/apache/apache2.4.9/conf/extra/httpd-vhosts.conf';

    // check if form has been submitted
    if (isset($_POST['vhostinput']))
    {
    // save the text contents
    file_put_contents($file, $_POST['vhostinput']);

    // redirect to form again
    header(sprintf('Location: %s', $url));
    printf('<a href="%s">Moved</a>.', htmlspecialchars($url));
    exit();
    }
    // read the textfile
    $text = file_get_contents($file);  
}


?>


<!-- Modal 1 -->
<div class="modal fade" id="modalhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Delete Host File Entry</h4>
        </div><!-- /modal-header -->
        <div class="modal-body">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-xs-12 col-md-12">
                        <!-- HTML form -->
                        <form action="" method="post">
                            <textarea id="hostinput" name="hostinput" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br />
                            <p><strong>NOTE:</strong> Ensure only lines similar to <kbd>127.0.0.1 www.dev.xxxxx</kbd> are deleted</p>
                    </div>
                </div>
            </div>                        
        </div><!-- /modal-body -->
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Save changes</button>
        </div> 
        </form>
    </div> <!-- /.modal-content -->
</div> <!-- /.modal-dialog -->

<!-- Modal 2 --> 
<div class="modal fade" id="modalvhost" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Delete VHOST File Entry</h4>
            </div><!-- /modal-header -->
            <div class="modal-body">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-xs-12 col-md-12">
                             <!-- HTML form -->
                             <form action="" method="post">
                                 <textarea id="vhostinput" name="vhostinput" class="form-control" rows="15"><?php echo htmlspecialchars($text) ?></textarea><br />
                        </div>
                    </div>
                </div>                        
            </div><!-- /modal-body -->
            <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Save changes</button>
             </div> 
            </form>
        </div> <!-- /.modal-content -->
     </div> <!-- /.modal-dialog -->
</div> <!-- /.modal -->
</div>  

我在第二次通话时遇到的错误是:     注意:未定义的变量:第116行的C:\ Users \ xxx \ xxx \ xxx \ devlogs \ delete.php中的文本

1 个答案:

答案 0 :(得分:1)

<button class="btn btn-primary btn-sm" data-target="#myModal1" data-toggle="modal" type="button"> Edit</button>  <!-- first button for the first modal-->

<button class="btn btn-primary btn-sm" data-target="#myModalA1" data-toggle="modal" type="button"> Comment</button>  <!-- second button for the second modal-->

<div id="myModal1" class="modal fade bs-example-modal-lg" aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1">
<div id="myModalA1" class="modal" aria-hidden="true" aria-labelledby="myModalLabel" role="dialog" tabindex="-1">

这段代码看起来与你的代码类似,但我不太确定是否存在一些差异(这个我100%肯定它的工作完全正常)。

现在针对您的问题,您要提交第二个模式的表单。有很多方法可以解决这个问题(例如使用ajax并保存所有内容而无需表单或刷新页面。)但是因为你说你是php的新手并且你已经以这种方式开始了所以......我可以建议的是该....

1)对于你拥有的每一个输入,你必须给它一个id和名字。所以你的输入应该是这样的(你可以为同一个字段设置相同的名称和id,但每个字段必须是唯一的):

<textarea class="form-control" rows="15" id="hostinput" name="hostinput"></textarea>
<textarea class="form-control" rows="15" id="vhostinput" name="vhostinput"></textarea>

2)然后在你的PHP代码中输入:

// check if form has been submitted
    if (!empty($_POST['hostinput'])){ //i prefer to use empty rather than isset you can read about it

    // save the text contents
    file_put_contents($file, $_POST['text']);

    // redirect to form again
    header(sprintf('Location: %s', $url));
    printf('<a href="%s">Moved</a>.', htmlspecialchars($url));
    exit();
}
esleif(!empty($_POST['vhostinput'])){
//SAVE THE VHOST
}

这样你正在做的是每当按下提交按钮时你试着检查这两个值并再次保存它们。

警告:这种方法有点危险....让我们说用户编辑了第一个文本,然后他点击取消....然后编辑了第二个文本然后他点击提交第二个文本....你实际上会保存它们......

我通常这样做的方法是使用javascript和ajax,所以我也不必刷新页面......

希望我帮助....