OctoberCMS 表单文件上传 |为 foreach() 提供的参数无效
OctoberCMS Form File upload | Invalid argument supplied for foreach()
我对 OctoberCMS 和 Laravel 还很陌生。我一直在设置联系表格,除了添加多个文件附件外,一切都完美无缺。我在网上进行了搜索,但未能找到我的表单无法发送的解决方案。
有问题的错误是"invalid argument supplied for foreach() on line 26"
第 26 行是第一个 foreach 循环
我在下面附上了表单 html 标记以及表单的控制器。
PHP代码
<?php
namespace Bprint\Contact\Components;
use Cms\Classes\ComponentBase;
use Input;
use Mail;
class ContactForm extends ComponentBase{
public function componentDetails(){
return [
'name' => 'Contact Form',
'description' => 'Bprint Contact Form'
];
}
public function onSend(){
$vars = ['fieldName' => Input::get('fieldName'), 'fieldCompany' => Input::get('fieldCompany'), 'fieldEmail' => Input::get('fieldEmail'), 'fieldUpload' => Input::file('fieldUpload'), 'fieldContent' => Input::get('fieldContent')];
$attachments = [];
foreach($vars['fieldUpload'] as $image) {
$attachments[] = $image;
}
Mail::send('bprint.contact::mail.message', $vars, function($message) use ($attachments) {
foreach($attachments as $attachment) {
$params = ['as' => $attachment->getFilename()/*, 'mime' => $mime*/];
$message->attach($attachment->getLocalPath(), $params);
}
$message->to('REDACTED', 'BlueprintCMS');
$message->subject('Blueprint Website | Contact Form"');
});
}
}
HTML 表单标记
<form data-request="onSend" data-request-loading="#loading" data-request-error="alert('There was an error sending your message. Please try again shortly, if error persits please email us at blueprint@outoftheblue.org.uk')" data-request-success="alert('Your message has been sent.')" class="bpsite-form floating-labels">
<fieldset>
<div class="icon">
<label class="bpsite-label" for="bpsiteName">Name</label>
<input name="fieldName" class="user" type="text" id="bpsite-name" required>
</div>
<div class="icon">
<label class="bpsite-label" for="bpsite-company">Company</label>
<input name="fieldCompany" class="company" type="text" id="bpsite-company">
</div>
<div class="icon">
<label class="bpsite-label" for="bpsite-email">Email</label>
<input name="fieldEmail" class="email" type="email" id="bpsite-email" required>
</div>
<input type="file" name="fieldUpload[]" multiple>
</fieldset>
<fieldset>
<div class="icon">
<label class="bpsite-label" for="bpsite-textarea"></label>
<textarea name="fieldContent" class="message" id="bpsite-textarea" required></textarea>
</div>
<div>
<button id="bpsite-button" style="display: none;" class="btn btn-primary form-btn" type="submit">Send</button>
<label for="bpsite-button">
<div class="box">
<span data-text="S">S</span>
<span data-text="E">E</span>
<span data-text="N">N</span>
<span data-text="D">D</span>
</div>
</label>
<div id="loading" style="display: none;">
<div class="col-2 col-sm-2 col-md-2 col-lg-2 col-xl-2">
<img class="img-fluid" src="{{ 'loader.gif'|media }}" alt="Loading..." />
</div>
<div class="col-10 col-sm-10 col-md-10 col-lg-10 col-xl-10">
<div class="alert alert-info">
<p class="body-text">Your message is being sent...</p>
</div>
</div>
</div>
</div>
</fieldset>
</form>
在此先感谢任何可以帮助我的人!
亲切的问候,
杰克
您正在使用 OctoberCMS 的 ajax 框架,并且您想使用此表单上传文件附件,那么您需要添加一个属性 data-request-files表单元素。
您的表单元素将如下所示:
<form data-request="onSend"
data-request-files
data-request-error="alert('There was an error sending your message. Please try again shortly, if error persits please email us at blueprint@outoftheblue.org.uk')"
data-request-success="alert('Your message has been sent.')"
class="bpsite-form floating-labels">
它会开始工作。
我对 OctoberCMS 和 Laravel 还很陌生。我一直在设置联系表格,除了添加多个文件附件外,一切都完美无缺。我在网上进行了搜索,但未能找到我的表单无法发送的解决方案。
有问题的错误是"invalid argument supplied for foreach() on line 26"
第 26 行是第一个 foreach 循环
我在下面附上了表单 html 标记以及表单的控制器。
PHP代码
<?php
namespace Bprint\Contact\Components;
use Cms\Classes\ComponentBase;
use Input;
use Mail;
class ContactForm extends ComponentBase{
public function componentDetails(){
return [
'name' => 'Contact Form',
'description' => 'Bprint Contact Form'
];
}
public function onSend(){
$vars = ['fieldName' => Input::get('fieldName'), 'fieldCompany' => Input::get('fieldCompany'), 'fieldEmail' => Input::get('fieldEmail'), 'fieldUpload' => Input::file('fieldUpload'), 'fieldContent' => Input::get('fieldContent')];
$attachments = [];
foreach($vars['fieldUpload'] as $image) {
$attachments[] = $image;
}
Mail::send('bprint.contact::mail.message', $vars, function($message) use ($attachments) {
foreach($attachments as $attachment) {
$params = ['as' => $attachment->getFilename()/*, 'mime' => $mime*/];
$message->attach($attachment->getLocalPath(), $params);
}
$message->to('REDACTED', 'BlueprintCMS');
$message->subject('Blueprint Website | Contact Form"');
});
}
}
HTML 表单标记
<form data-request="onSend" data-request-loading="#loading" data-request-error="alert('There was an error sending your message. Please try again shortly, if error persits please email us at blueprint@outoftheblue.org.uk')" data-request-success="alert('Your message has been sent.')" class="bpsite-form floating-labels">
<fieldset>
<div class="icon">
<label class="bpsite-label" for="bpsiteName">Name</label>
<input name="fieldName" class="user" type="text" id="bpsite-name" required>
</div>
<div class="icon">
<label class="bpsite-label" for="bpsite-company">Company</label>
<input name="fieldCompany" class="company" type="text" id="bpsite-company">
</div>
<div class="icon">
<label class="bpsite-label" for="bpsite-email">Email</label>
<input name="fieldEmail" class="email" type="email" id="bpsite-email" required>
</div>
<input type="file" name="fieldUpload[]" multiple>
</fieldset>
<fieldset>
<div class="icon">
<label class="bpsite-label" for="bpsite-textarea"></label>
<textarea name="fieldContent" class="message" id="bpsite-textarea" required></textarea>
</div>
<div>
<button id="bpsite-button" style="display: none;" class="btn btn-primary form-btn" type="submit">Send</button>
<label for="bpsite-button">
<div class="box">
<span data-text="S">S</span>
<span data-text="E">E</span>
<span data-text="N">N</span>
<span data-text="D">D</span>
</div>
</label>
<div id="loading" style="display: none;">
<div class="col-2 col-sm-2 col-md-2 col-lg-2 col-xl-2">
<img class="img-fluid" src="{{ 'loader.gif'|media }}" alt="Loading..." />
</div>
<div class="col-10 col-sm-10 col-md-10 col-lg-10 col-xl-10">
<div class="alert alert-info">
<p class="body-text">Your message is being sent...</p>
</div>
</div>
</div>
</div>
</fieldset>
</form>
在此先感谢任何可以帮助我的人!
亲切的问候, 杰克
您正在使用 OctoberCMS 的 ajax 框架,并且您想使用此表单上传文件附件,那么您需要添加一个属性 data-request-files表单元素。
您的表单元素将如下所示:
<form data-request="onSend"
data-request-files
data-request-error="alert('There was an error sending your message. Please try again shortly, if error persits please email us at blueprint@outoftheblue.org.uk')"
data-request-success="alert('Your message has been sent.')"
class="bpsite-form floating-labels">
它会开始工作。