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


Two Bootstrap Modals to edit two different files content

首先让我说,我知道这将是一个非常基本的问题。我是PHP的新手,在这个任务上有点吃力。

简介:我正在本地WAMP服务器上制作一个小应用程序,以管理基本的客户端数据库,并管理我所有的VHOST enntrys进行开发。我知道根据我的请求编辑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>,这将打开引导模态,在文本区域中显示Windows主机文件,并允许我随意添加和删除,我单击模态的保存按钮,它就工作了。

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

我只是不知道如何复制第一个正确工作的PHP,现在也可以使用VHOST编辑要求。

任何帮助或建议都将不胜感激。

编辑

嗨,尼尔,我试着把你的解决方案落实到位,但遇到了一些错误,我不完全确定如何克服。。。

我目前在我的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>  

我在第二次通话中得到的错误是:注意:未定义的变量:C:''Users''xxx''xxxx''devlogs''delete.php中的文本,位于第116行

<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,保存所有内容而不需要表单或刷新页面。。。。

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,这样我就不必刷新页面了。。。

希望我能帮上忙。。。。