【问题标题】:Store text from modal input field in session在会话中存储来自模态输入字段的文本
【发布时间】:2016-05-13 07:51:27
【问题描述】:

我将来自 mysql 的数据存储在 SESSION 中,并将其传递到另一个页面,在该页面中我使用所有数据制作 zip 并下载它。到目前为止,一切都很完美。

现在我正在尝试在用户单击下载按钮之前进行实际下载以在模式窗口中输入 zip 存档的名称,然后下载存档。到目前为止,我已经制作了模式,但现在我无法弄清楚如何将输入字段文本传递到会话中。所有其他数据都正确传递。这是到目前为止的来源

if(! isset($_POST['showcart'])) exit;
echo '<a class="btn btn-danger btn-lg pull-right" style="margin: 10px;" data-toggle="modal" data-target="#myModalNorm">Download All Files</a>
<table class="table table-striped table-bordered table-condensed responsive" id="sort">
                    <thead>
                <tr>
                    <th>ID</th>
                    <th>Title</th>                  
                    <th>Description</th>               
                    <th>Action</th>
                </tr>
                    </thead>';
foreach ($_SESSION['itemid'] as $i):

    $sql = "SELECT * FROM uploads WHERE upload_id = :id"; 
    $result = $pdo->prepare($sql);
    $result->bindParam(":id", $i);
    $result->execute();                 

    $resArray = $result->fetchAll();

     foreach ( $resArray as $row ):?>

        <div class="row">
            <div class="box-content">
                <tbody>
        <tr>    
            <td class="center"><?=$row["upload_id"]?></td>
            <td class="center"><?=$row["upload_title"]?></td>                    
            <td class="center"><?=$row["upload_description"]?></td>
        </tr>
                </tbody>
            </div>
        </div>
        <!-- Modal -->
        <div class="modal fade" id="myModalNorm" tabindex="-1" role="dialog" 
             aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <!-- Modal Header -->
                    <div class="modal-header">
                        <button type="button" class="close" 
                           data-dismiss="modal">
                               <span aria-hidden="true">&times;</span>
                               <span class="sr-only">Close</span>
                        </button>
                        <h4 class="modal-title" id="myModalLabel">
                            Please enter the name for the archive
                        </h4>
                    </div>

                    <!-- Modal Body -->
                    <div class="modal-body">

                        <form role="form">
                          <div class="form-group">
                            <label for="exampleInputEmail1"></label>
                              <input type="text" class="form-control"
                              id="archiveName" placeholder="Please enter the name for the archive"/>
                          </div>

                        </form>


                    </div>

                    <!-- Modal Footer -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default"
                                data-dismiss="modal">
                                    Close
                        </button>
                        <a href="create_zip.php" class="btn btn-primary">
                            Download
                        </a>
                    </div>
                </div>
            </div>
        </div>                                       


    <?php endforeach;?>

 <?php endforeach; ?>
 </table> </div>

create_zip.php 上,我从$_SESSION['itemid'] 获取所有信息,但这个&lt;input type="text" class="form-control" id="archiveName" placeholder="Please enter the name for the archive"/&gt;

如何也接受这个输入?

【问题讨论】:

  • 将其作为调用 create_zip.php onClick 的 AJAX 按钮执行,并传递 archiveName 字段,或者您可以挂钩到 click 事件并将 archiveName 的值作为 _GET 参数传递。跨度>
  • 我怎样才能使用 AJAX do it either as an AJAX button which calls create_zip.php onClick, passing the archiveName field with it

标签: php session


【解决方案1】:

实际上,您可以很容易地做到这一点。首先将您的桌子包裹在&lt;form&gt;&lt;/form&gt;周围 所以会发生这样的事情

if(! isset($_POST['showcart'])) exit;
echo '<form action="create_zip.php" method="post">
         <a class="btn btn-danger btn-lg pull-right" style="margin: 10px;" data-toggle="modal" data-target="#myModalNorm">Download All Files</a>
      <table class="table table-striped table-bordered table-condensed responsive" id="sort">
         <thead>
           ....
                    <!-- Modal Body -->
                    <div class="modal-body">

                          <div class="form-group">
                            <label for="archiveName"></label>
                              <input type="text" class="form-control"
                              id="archiveName" name="archiveName" placeholder="Please enter the name for the archive"/>
                          </div>

                    </div>
                    <!-- Modal Footer -->
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default"
                                data-dismiss="modal">
                                    Close

                        <input type="submit" name="submit" value="Download" class="btn btn-primary">

                    </div>
                </div>
            </div>

        </div>                                       

    <?php endforeach;?>

 <?php endforeach; ?>
</table> </div></form>

然后在create_zip.php 中检查是否 isset 提交按钮并分配变量。

if(isset($_POST['submit']))
{   
     //your code
     $archiveName = $_POST['archiveName'];

     // other source
} else { echo 'no archive'; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2018-09-22
    相关资源
    最近更新 更多