在 HTML 表单中使用多个选择框 [重复]
Posted
技术标签:
【中文标题】在 HTML 表单中使用多个选择框 [重复]【英文标题】:Using Multiple Select Boxes in HTML Form [duplicate] 【发布时间】:2017-05-22 16:20:39 【问题描述】:我是 php 编码的初学者。我正在为一个沙龙网站工作,该网站有一个表单,用户可以在其中以发送电子邮件的形式预订约会,我正在尝试从我在 html 表单中的多个选择框中发布值并发送该数据以及电子邮件中的其他字段。其他字段都可以发布。但是选择框不起作用。以下是我正在使用的代码:
<?php
$clnt_name = $_POST['w_p_c_name'];
$clnt_email = $_POST['w_p_c_email'];
$clnt_phn_no = $_POST['w_p_c_number'];
$rsrvtn_date = $_POST['w_p_c_date'];
$hair_cut = $POST['opt_hair_cut'];
$colr_whl_hair = $POST['opt_colouring_whole_hair'];
$colr_retouch = $POST['opt_colouring_retouch_roots'];
$colr_highlight = $POST['opt_colouring_highlighting'];
$rebonding = $POST['opt_rebonding'];
$relax_straight = $POST['opt_relaxing_straightening'];
$perming = $POST['opt_perming'];
$threading = $POST['opt_threading'];
$bleaching = $POST['opt_bleaching'];
$manicure = $POST['opt_manicure'];
$pedicure = $POST['opt_pedicure'];
$nail = $POST['opt_nail'];
$gel_nail = $POST['opt_gel_nail'];
$massage = $POST['opt_massage'];
$scrub = $POST['opt_scrub'];
$wax_face = $POST['opt_wax_face'];
$wax_arms = $POST['opt_wax_arms'];
$wax_leg = $POST['opt_wax_leg'];
$wax_body = $POST['opt_wax_body'];
$wax_intimate = $POST['opt_wax_intimate'];
$makeup = $POST['opt_makeup'];
$hijab = $POST['opt_hijab'];
$hairstyle_blodry = $POST['opt_hairstyle_blowdry'];
$hairstyle_iron = $POST['opt_hairstyle_iron'];
$hairstyle_spcl = $POST['opt_hairstyle_special'];
$hair_trmnt = $POST['opt_hair_treatment'];
$face_cleaning = $POST['opt_face_cleaning'];
$facials = $POST['opt_facials'];
$face_trmnt = $POST['opt_face_treatments'];
$scrub_ladies = $POST['opt_scrubs_ladies'];
$massage_ladies = $POST['opt_massage_ladies'];
$data = "Name : ".$clnt_name."\nEmail : ".$clnt_email."\nPhone : ".$clnt_phn_no."\nRequested Date : ".$rsrvtn_date."\nServices Requested :\n\n".$hair_cut."\n".$colr_whl_hair."\n".$colr_retouch."\n".$colr_highlight."\n".$rebonding."\n".$relax_straight."\n".$perming."\n".$threading."\n".$bleaching."\n".$manicure."\n".$pedicure."\n".$nail."\n".$gel_nail."\n".$massage."\n".$scrub."\n".$wax_face."\n".$wax_arms."\n".$wax_leg."\n".$wax_body."\n".$wax_intimate."\n".$makeup."\n".$hijab."\n".$hairstyle_blodry."\n".$hairstyle_iron."\n".$hairstyle_spcl."\n".$hair_trmnt."\n".$face_cleaning."\n".$facials."\n".$face_trmnt."\n".$scrub_ladies."\n".$massage_ladies;
$file = "services.xlxs";
$subject = "Reservation Booking";
$mail_message1 = "Hi ".$clnt_name." We have received your request for a reservation with the following details.\n\n".$data."\n\nWe will be calling to confirm the reservation shortly.\n\nThank you";
$to_rsvtn = "reservation@uxanisalon.com";
$subject2 = "Reservation";
$mail_message2 = "There has been a new request for a reservation. Details are listed below: \n\n".$data;
file_put_contents($file, $data1 . PHP_EOL, FILE_APPEND);
file_put_contents($file, $data2 . PHP_EOL, FILE_APPEND);
file_put_contents($file, $data3 . PHP_EOL, FILE_APPEND);
mail($clnt_email, $subject, $mail_message1, "From:" . $to_rsvtn);
mail($to_rsvtn, $subject2, $mail_message2, "From:" . $to_rsvtn);
header('Location: ba-complete.html');
?>
非常感谢任何帮助。
【问题讨论】:
能否请您也发布html代码? 如果未选中复选框,则不会发送该值。您的服务器需要根据传递的值将复选框设置为值或空 “其他字段 POST-ing 很好。” - 我觉得这很难相信,看到很多$POST
是无效的。
【参考方案1】:
添加 HTML 代码确实会有所帮助。
如果您希望 PHP 将 $_POST['select']
视为一个选项数组,只需在 select 元素的名称中添加方括号,如下所示:<select name="select[]" multiple …
然后你可以在你的 PHP 脚本中访问数组
<?php
foreach ($_POST['select'] as $selectedOption)
echo $selectedOption."\n";
?>
【讨论】:
是的,但您的回答未能概述许多$POST
的使用,这是不正确的。以上是关于在 HTML 表单中使用多个选择框 [重复]的主要内容,如果未能解决你的问题,请参考以下文章