间接单击提交按钮不会设置$ _POST ['submit']

时间:2019-05-29 09:01:04

标签: javascript php jquery html forms

我正在尝试将图像插入到我的文件系统中: 用户单击按钮时,将触发表单的文件输入;选择文件时,将触发对表单提交按钮的单击,并将其发布到PHP处理脚本中。

当前脚本无法识别$ _POST ['submit']已被设置。我不知道为什么吗?

HTML:

<div id="toolbar">
            <!-- custom toolbar: linking all buttons to Quill -->
            <span class="ql-formats">
                 <button class="ql-clean"></button>
                 <button class="ql-upload"><i class="fas fa-images"></i></button>
            </span>
</div>

<form action="upload.php" method="post" enctype="multipart/form-data" id="form1" runat="server" name="imageForm">
    <input type="file" name="file" id="imgInp">
    <button type="submit" id="sub2" name="submit">Save</button>
    <span id="result"></span>
</form>

JS:

//linking button to input field
$('.ql-upload').click(function()
{
    $("#imgInp").trigger("click");
});

// submitting form when file is chosen
$(document).ready(function()
{
    document.getElementById("imgInp").onchange = function()
    {
        $("#sub2").trigger("click");
    };
});

PHP:

session_start();
require_once('db.php');

if (isset($_POST['submit'])) {
    $file = $_FILES['file'];

    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];
........ etc

1 个答案:

答案 0 :(得分:1)

代替检查是否设置了$_POST['submit']。我建议检查请求是否为POST。

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // ... Handle your form
}