【问题标题】:How to merge two input that are in different forms如何合并两个不同形式的输入
【发布时间】:2019-11-07 15:52:23
【问题描述】:

我有一个上传文件的索引页面。一个输入按钮用于上传文件,另一个按钮将成绩导入外部系统。要导入成绩,用户应经过身份验证。因此,我创建了两个表单,每个按钮都在一个表单中。但是,它不起作用,因为第一次上传成绩并单击导入成绩,它工作得很好。第二次,当我点击“上传文件”按钮时,它会同时进行导入分级。我如何将这些操作分开,或者更好的是,将这两个操作添加到同一个按钮中?

我已经尝试通过将action="authenticateUser.php" 添加到第一个form 并删除第二个form 来合并这两个表单,但它不起作用。

<title>Import Grades Widget</title>

<form action="" method="POST" enctype="multipart/form-data">
  <labeL class="tool-actions"> <span>Choose CSV File:</span></labeL>

  <input class="tool-actions" type="file" accept="text/csv" id='uploadfile' name="userfile" />
  <p>
    <!--add space btw buttons -->
    <br>
    <input class="btn-primary" type="submit" name="send" id="btnSend" value="Upload file" />

    <labeL class="tool-actions"> <span></span></labeL>
    <?php
       require_once "upload.php";                
     ?>
    <p id="info-message-crn" style="color:red"></p>
</form>

<form method="POST" enctype="multipart/form-data" action="authenticateUser.php" id="configForm">
  <input class="btn-primary" type="submit" id="btnImport" name="btnImport" value="Import Grades" />
  <?php 
  if(file_exists("uploads/uploadFile.csv") AND isset($_POST['btnImport'])) {  
      require_once ('apiCalls.php');  
  }else {
      ?>
  <p style="color:grey" id="info-message">
    <?php echo "Select a csv file.";?>
  </p>

  <?php
  }
  ?>
</form>

【问题讨论】:

  • 您需要决定是要将这些操作分开还是将它们合并到一个步骤中。如果您想将它们分开,那么您有几个选择 - stackoverflow.com/questions/14071250/…。如果您想将这两个操作保持在一个步骤中,那么我将使用一个名为 Upload and Process 的表单,该表单负责上传并在之后直接处理数据。
  • @waterloomatt 由于我有第二种形式的操作,我该如何合并?当我添加操作(即authenticateUser.php)时,我的上传不再起作用。
  • 请帮忙!我试图分开,因为我将 authenticateUser.php 作为第二种形式的操作,但它不起作用。有什么建议吗?
  • 另外,为什么不在每个表单上设置不同的action?让您的上传表单将其数据发布到 upload.php,流程表单将其数据发布到 process.php。表单中不要有任何 include 调用——这会使事情复杂化。

标签: php html forms input


【解决方案1】:

我会改变你布局逻辑的方式,因为它目前有点太复杂了。使用 3 个简单的文件让事情变得非常简单。

  • index.php - 仅呈现表单的主“显示”文件。它什么都不做。
  • upload.php - 处理上传部分并重定向回 index.php
  • process.php - 验证用户身份,处理所有待处理的文件,然后重定向回 index.php

请注意,每个表单都将数据发布到自己的 handlerupload.phpprocess.php)。它使用表单的 action 属性来完成此操作。该处理程序负责上传或处理待处理文件的实际逻辑。

处理程序完成其工作后,它会重定向回 index.php,它将呈现上传表单,如果合适,还将呈现流程表单。

index.php

// Notice the actions on the forms. Each form sends data to its own handler
<form name="upload" method="post" action="upload.php" enctype="multipart/form-data">
    ...
    // Don't include any logic in here - only inputs and buttons.
    // All the upload logic is in upload.php
</form>

// Only show this form if there are pending files to process
// ** Side note - this could just be a link which points to process.php
// ** It does not need to be a form. 
if (exists (pending file(s) to process) {
    <form name="process" method="post" action="process.php">
        ...
        // Don't include any logic in here - only inputs and buttons.
        // All the processing logic is in process.php
    </form>
}

上传.php

<?php
if (request method === POST) {
    upload file logic
}

redirect back to index.php

process.php

<?php
if (request method === POST) {
    authenticate user - if !authenticated > redirect to login
    process the file 
}

redirect back to index.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多