繁体   English   中英

在联系表单上将文件上载字段设为可选

[英]Make File Upload Field Optional on Contact Form

我有一个基本的HTML / PHP Web表单。 当我填写所有字段时,一切正常,除了我希望使文件上传为可选,而不是必需。 我已经从html表单中删除了“ required”,以进行上传。 我检查了我的handler.php,但没有说明该字段是必填字段。 我检查了formhandler.php,它说附件为空。 我已经看过类似的问题,但是我可能实施的任何更复杂的解决方案都不正确。 我已经花了我的PHP知识寻找解决方案。 我想念什么?

这是表格的html:

<div class="row pb6">
                <div class="col-md-6  offset-md-3">
                    <form role="form" method="post" id="reused_form" enctype="multipart/form-data" >
                        <div class="form-group">
                            <label for="name"> Name:</label>
                            <input type="text" class="form-control" id="name" name="name" required maxlength="50">
                        </div>
                        <div class="form-group">
                            <label for="email"> Email:</label>
                            <input type="email" class="form-control" id="email" name="email" required maxlength="50">
                        </div>
                        <div class="form-group">
                            <label for="tel"> Phone: </label>
                            <input type="tel" class="form-control" id="tel" name="tel" maxlength="50">
                        </div>
                        <div class="form-group">
                            <label for="name"> Message:</label>
                            <textarea class="form-control" type="textarea" name="message" id="message" placeholder="We want to hear all about your performance! Also, please include the date you need the music." maxlength="6000" rows="7"></textarea>
                        </div>
                        <div class="form-group">
                            <label for="file">File Upload (optional)</label>
                            <input type="file" class="form-control" id="image" name="image">
                        </div>
                        <button type="submit" class="btn btn-lg btn-success pull-right" id="btnContactUs">Post It! &rarr;</button>
                    </form>
                    <div id="success_message" style="width:100%; height:100%; display:none; "> <h3>Sent your message successfully!</h3> </div>
                    <div id="error_message" style="width:100%; height:100%; display:none; "><h3>Error</h3>We are very sorry. There was an error sending your form. Email us and we will send you a free demo.</div>
                </div>
            </div>
        </div>

这是handler.php:

    ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )

 */
require_once './vendor/autoload.php';

use FormGuide\Handlx\FormHandler;


$pp = new FormHandler(); 

$validator = $pp->getValidator();
$validator->fields(['name','email','tel'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);


if($_FILES['image']){ $pp->attachFiles(['image']); }


$pp->sendEmailTo('email'); //

echo $pp->process($_POST);

编辑代码后:1)Chrome和Firefox将发送表单,而Safari将不会发送表单。 2)所有浏览器都显示永久的“正在发送”消息,而不显示该表单的成功或失败消息。 3)带有WITH附件发送的表单将不会发送带有电子邮件附件的表单,而只会发送表单。

您正在尝试使用$pp->attachFiles(['image']);附加映像是否存在$pp->attachFiles(['image']); 您应该首先检查它是否确实存在,并且仅在存在时才附加它:

if($_FILES['image']){ 
$pp->attachFiles(['image']); 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM