【发布时间】:2018-07-18 10:57:48
【问题描述】:
我有一个基本的 HTML/PHP 网络表单。当我填写所有字段时,一切正常,除了我想让文件上传可选,不是必需的。我已经从表单 html 中删除了“必需”以进行上传。我检查了我的 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! →</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) 发送带附件的表格不会将带有附件的表格发送到电子邮件,仅发送表格。
【问题讨论】:
-
是的,你需要点击
{}而不是""来格式化PHP -
如果没有实际图像,您可能不应该致电
$pp->attachFiles(['image']);。 -
if($_FILES['image']){ $pp->attachFiles(['image']); }
-
Sanjay Kumar,谢谢你,我用你的代码替换了 php 的第 22 行(在最后一个验证器行和 sendEmailTo 行之间),它不起作用。我应该把它放在别的地方吗?
标签: php html forms file-upload