php - 如何在 laravel 中使用一个选择选项发送多个数据?

标签 php database forms laravel

我想发送带有选择选项的多个数据。示例:理发的选项是长的还是小的。如果你是男性,则应为 20 分钟,女性为 30 分钟。

现在我有一个包含多个数据的表单: 第一个值是名称。第二个是男人的时间,第三个是女人的时间。

<div class="radio">
    <label>
        <input type="radio" name="hairstyle" id="optionsRadios2" value="Kort|20|30">Kort
    </label>
</div>

<div class="radio">
    <label>
        <input type="radio" name="haircolor" value="Wit|20|30">Wit
    </label>
</div>

在我的存储函数中,我剥离数据并根据性别更新它

public function store(Request $request)
{
    $tasks = Appointment::create($request->all());

    /* Get gender value */
    $gender = $request->input('gender');

    /* Get hair options values */
    $hairstyle = $request->input('hairstyle');
    $haircolor = $request->input('haircolor'); 

    /* Strip hair options values */
    $hs_values = explode('|', $hairstyle);
    $hairstyle_choice = $hs_values[0];
    $hairstyle_time_men = $hs_values[1];
    $hairstyle_time_woman = $hs_values[2];

    $hc_values = explode('|', $haircolor);
    $haircolor_choice = $hc_values[0];
    $haircolor_time_men = $hc_values[1];
    $haircolor_time_woman = $hc_values[2];

    /* Check if gender = men */            
    if ($gender == 'men'){

        $time = $hairstyle_time_men + $haircolor_time_men;

    }
    /* Woman */
    else{

        $time = $hairstyle_time_woman + $haircolor_time_woman;

    }

    /* Update the new values in the row with id .. */
    $titles = DB::table('appointments')
    ->where('id', $tasks->id)
    ->update([
        'hairstyle' => $hairstyle_choice,
        'haircolor' => $haircolor_choice,
        'time_costs' => $time,
    ]);

    return redirect( '/appointments' );

}

如您所见,我去掉了 | 并将所有内容保存在它自己的变量中。然后我检查它是设置为男性还是女性。在此基础上,我添加了男性或女性的时间并更新了该行。

这行得通,但我认为它很困惑。这是我应该更改的内容吗,有人可以给我一个示例或解释我需要更改的内容吗?

最佳答案

首先,您不应该从前端向后端发送时间常数。您最好在后端的某个地方对它们进行硬编码。在我的示例中,我只是在相同的 Controller 方法中将它们硬编码为数组。

从前端您应该只发送所选发型/发色类型的名称。例如 kortwit。我还建议您对这些值使用小写字母。这是 Controller 操作的样子:

public function store(Request $request)
{
    $tasks = Appointment::create($request->all());

    /* Get the values */
    $gender = $request->input('gender');
    $hairstyle = $request->input('hairstyle');
    $haircolor = $request->input('haircolor'); 

    // Predefined constants
    $hairstyle_times = [
        'men' => [
            'kort' => 20,
            'something_else' => 30,
            //...etc
        ],
        'women' => [
            'kort' => 30,
            'something_else' => 40,
            //...etc
        ],

    ];

    $haircolor_times = [
        'men' => [
            'wit' => 20,
            'something_else' => 30,
            //...etc
        ],
        'women' => [
            'wit' => 30,
            'something_else' => 40,
            //...etc
        ]
    ];

    //Calculate the time and save the appointment
    $time = $hairstyle_times[$gender][$hairstyle] + $haircolor_times[$gender][$haircolor];

    /* Update the new values in the row with id .. */
    $titles = DB::table('appointments')
    ->where('id', $tasks->id)
    ->update([
        'hairstyle' => $hairstyle_choice,
        'haircolor' => $haircolor_choice,
        'time_costs' => $time,
    ]);

    return redirect( '/appointments' );

}

这样定制起来就很容易了。 | 不再有魔力了。此外,高级用户将无法输入他们的时间值并将其发送到后端。

关于php - 如何在 laravel 中使用一个选择选项发送多个数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39493424/

相关文章:

php - Web 应用程序的最佳工作流程是什么?

database - 从数据库模式创建 Web 表单

html - 在 CSS 中将提交按钮作为表单的扩展

php - 奇怪的 PDO 行为

php - 如何在没有 Composer 作为依赖项的情况下使用具有命名空间的 PHP 库(PSR-0)?

MySQL - 将来自同一个表的多个选择组合成一个结果表与一个组

mysql - Mysql搜索查询优化

jquery - 验证 jQuery 插件,字段不是必需的

forms - dompdf 表单在安全服务器 (ssl) 中无法正常工作

javascript - 为不同的 Url 重写 htaccess 规则