dou448172583 2017-04-06 20:21
浏览 93
已采纳

HTML表单,2个提交样式按钮,用于2个不同的PHP动作/文件

I have an HTML file upload form that accepts a file and submits it to a database through PHP, which works fine. The code:

<form method="post" action="confirm.php" enctype="multipart/form-data">
 <label>Upload File</label>
 <input type="file" name="file">
 <input type="submit" name="submit" value="Preview" formtarget="_blank">
</form>

My issue is that i need to combine the preview php and the submission php. Both work independently using the $file but I'm trying to make it so the user uploads -> hits the preview button -> previews the info -> hits submit -> info goes into database, but the upload php will say the file is invalid. I used some PHP code to display the contents in an html table to verify the information before submitting it to the database, like so:

<body>
<div class="phpTableWrap">
<?php

$server = "localhost";
$user = "root";
$pw = "root";
$db = "uwsTest";

$connect = mysqli_connect($server, $user, $pw, $db);

if ($connect->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
    echo'success!';
}

if(isset($_POST['submit']))
{
ini_set('auto_detect_line_endings', true);

$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
$maxPreviewRows = PHP_INT_MAX;  // this will be ~2 billion on 32-bit system, or ~9 quintillion on 64-bit system
$hasHeaderRow = true;

    echo '<table>';
    if ($hasHeaderRow) {
        $headerRow = fgetcsv($handle);
        echo '<thead><tr>';
        foreach($headerRow as $value) {
            echo "<th>$value</th>";
        }
        echo '</tr></thead>';
    }
    echo '<tbody>';
    $rowCount = 0;
    while ($row = fgetcsv($handle)) {
        echo '<tr>';
        foreach($row as $value) {
            echo "<td>$value</td>";
        }
        echo '</tr>';

        if (++$rowCount > $maxPreviewRows) {
            break;
        }
    }
    echo '</tbody></table>';
   }
?>
</div>

Is there a way to share the uploaded file with both PHP files so that I can insert data from the preview page?

  • 写回答

1条回答 默认 最新

  • duandou8120 2017-04-06 23:00
    关注

    Make your preview button add a hidden field like <input type='hidden' name='action' value='preview' /> with javascript before submitting and detect that in your PHP file:

    if ($_POST['action'] == 'preview') {
     // do preview stuff
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料