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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名