dsfdsf48652 2019-06-25 18:47
浏览 138

如何合并两个不同形式的输入

I have an index page that upload a file. An input button does the upload of the file and another import the grades to an external system. To import the grades, the user should be authenticated. Because of this, I have create two forms, and each button is in a form. However, it is not working because the first time that upload a grade and click import grades, it works just fine. The second time, when I click on "Upload file" button, it does the import grade at the same time. How I do to separate this actions, or better, add those two action in a same button?

I already tried to merge both forms by adding action="authenticateUser.php" to the first form and remove the second form, but it doesn't work.

<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>
  • 写回答

1条回答 默认 最新

  • dongyan8896 2019-06-27 12:27
    关注

    I'd change the way you layout your logic because it is currently a bit too complicated. Keep things very simple with 3 simple files.

    • index.php - the main "display" file which only renders the forms. It does nothing else.
    • upload.php - handles the upload portion and redirects back to index.php
    • process.php - authenticates a user, processes any pending files, then redirects back to index.php

    Notice that each form posts data to its own handler (upload.php and process.php). It does this with the action attribute of the form. That handler is responsible for the actual logic of either uploading or processing a pending file.

    After the handler has finished its work, it redirects back to index.php which will render the upload form, and if appropriate, will also render the process form.

    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>
    }
    

    upload.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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)