【问题标题】:Send array data to email through Laravel Mailable通过 Laravel Mailable 将数组数据发送到电子邮件
【发布时间】:2020-02-24 02:57:15
【问题描述】:

create.blade.php(我的表单)有以下几行

<form action="/contact" method="POST">
    <div class="form-control">

<fieldset>
    <legend><span class="number">1</span> Personal Information</legend>


        <input type="text" name="name" placeholder="Name *" value="{{old('name') }}">
        <div>{{ $errors->first('name') }} </div>

        <input type="text" name="email" placeholder="Phone *" value="{{old("email") }}">
        <div>{{ $errors->first('email') }} </div>

        <input type="text" name="phone" placeholder="Phone *" value="{{old('phone') }}">
        <div>{{ $errors->first('phone') }} </div>   
        <label for="message">Message</label>
        <textarea type="text" name="message" id="message" cols="30" rows="10">{{old('message') }} 
      </textarea>
        <div>{{ $errors->first('message') }} </div>

<?php
$countries = array('India' => 'India',
        'Pakistan' => 'Pakistan',
        'Bosnia' => 'Bosnia',
        'Andorra' => 'Andora',
        'Haiti' => 'Haiti',
        'Australia' => 'Australia',
        'Maldives' => 'Maldives'
        );
?>

<label for="message">Country</label>

<select name="country">
    @foreach ($countries as $country)
        <option value="{{ $country }}" @if(old('country') == $country) selected="selected" @endif>{{ 
$country }}</option>
    @endforeach
</select>
@csrf
    <button type="submit" class="btn btn-primary">Send message</button>
</div>
</form>

对于我的contact-form.blade.php(可邮寄),我有以下内容。这应该在我收到的电子邮件中显示提交的数据。问题是我在输入字段(姓名、号码电子邮件)中获取了所有数据,但我没有得到所选国家/地区的数据。问题很可能在于以下几行。

 @component('mail::message')

#Requested Information

<strong>Name</strong> {{ $data['name'] }}
<strong>Email</strong> {{ $data['email'] }}

<strong>Message</strong> {{ $data['message'] }}

<strong>Country</strong> {{ $data['$country'] }}

@endcomponent

【问题讨论】:

  • $data['$country'] 得到了什么?你试过去掉$吗?
  • 即使在删除它之后也会出现错误。错误异常 {#272 ▶}
  • $data 的输出是什么?为什么你不能使用请求?然后你可以简单地使用$request-&gt;country...
  • 我正在制作一个国家的下拉列表。用户在表格中选择一个国家。收到邮件后,所选国家/地区必须出现在我的收件箱中。请参阅我已将国家/地区列表作为数组放置的问题。预期的输出在我的 Mailtrap 中,我必须收到带有所选国家/地区的消息。我只得到其他字段,如姓名、电子邮件和电话。
  • 预期的输出是所选国家/地区的名称(来自下拉列表)必须出现在我到达 mailtrap 的 Markdown 邮件中。问题仅在于下拉列表。其他输入工作正常。

标签: php arrays laravel forms


【解决方案1】:

正确的是$data['country'] 不是$data['$country']

【讨论】:

  • ErrorException (E_ERROR) 未定义索引:国家(查看:C:\xampp\htdocs\lsapp\resources\views\emails\contact\contact-form.blade.php)
  • 这个$data是一个变量$country。 UNLIKE @data“名称”和“电子邮件”。问题是我不能发送这个选定的国家(从​​下拉列表)没有出现在 Markdown 邮件中。除了将这个选定的输入从下拉列表发送到 Markdown 邮件之外,所有设置都已设置。姓名和电子邮件输入进入收件箱。
【解决方案2】:

我认为您正在寻找以下代码

 $data=array(
        'Name' => $request->name,
        'email' => $request->email,
        'country'=>$request->country,
        'phone'=> $rwquest->phone,
    );

    Mail::send('here your email filename', $data, function($message)  {
        $message->subject('');   //subject of email
        $message->from();   // sender email address
        $message->to();   //receiver email
    });

【讨论】:

  • 姓名和电子邮件不在数组中(只是一个文本输入字段)国家列表是数组。这是我的 Markdown 邮件文件代码 @component('mail::message') #Q​​uotation Request Name {{ $data['name'] }} Email {{ $data['email'] }} 电话 {{ $data['phone'] }} 消息{{ $data['message'] }} Country XXXXXX @endcomponent 需要正确填写XXXXX部分
【解决方案3】:

我已插入以下行

$countries = $_POST['country'];

现在是这个样子

<select>
    $countries = $_POST['country'];

@foreach ($countries as $country)
<option value="{{ $country }}" name="country"

@if(old('country') == $country) selected="selected" 
@endif>

{{ $country }}</option>
@endforeach
</select>

我的邮件中还有以下行

Country {{ $data['country'] }}

我得到错误 undefined index:country

【讨论】:

    猜你喜欢
    • 2020-04-16
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 2019-01-13
    • 2018-03-21
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多