dongtang1909 2015-06-17 23:45
浏览 23

<选择多个>不使用PHP [重复]在html表单上工作

This question already has an answer here:

Okay, so I have a html form, and a using php for the server side validation to send the information from the form as an email. I cannot, for the life of me, get the form to send the items as an array.

Here's my html:

   <select multiple name="symptoms[]">
   <option value="ADHD">ADHD</option>
   <option value="Insomnia">Insomnia</option>
   <option value="Depression">Depression</option>
   <option value="PTSD">PTSD</option>
   <option value="Autism">Autism</option>
   <option value="Stress">Stress</option>
   <option value="Addiction">Addiction</option>
   <option value="Pain">Pain/Headaches</option>

Here's my php in which I think the problem lies:

// validation expected data exists

if(!isset($_POST['first_name']) ||

    !isset($_POST['last_name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['telephone']) ||

    !isset($_POST['symptoms']) ||

    !isset($_POST['location']) ||

    !isset($_POST['comment'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');       

}

$first_name = $_POST['first_name']; // required

$last_name = $_POST['last_name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['telephone']; // not required

$symptoms = $_POST['symptoms']; // not required

$location = $_POST['location']; // not required

$comments = $_POST['comment']; // required

There is also this section of php, but I don't think the 'symptoms' area is a problem here...

function clean_string($string) {

  $bad = array("content-type","bcc:","to:","cc:","href");

  return str_replace($bad,"",$string);

}

$email_message .= "First Name: ".clean_string($first_name)."
";

$email_message .= "Last Name: ".clean_string($last_name)."
";

$email_message .= "Email: ".clean_string($email_from)."
";

$email_message .= "Telephone: ".clean_string($telephone)."
";

$email_message .= "Symptoms: ".clean_string($symptoms)."
";

$email_message .= "Location: ".clean_string($location)."
";

$email_message .= "Comment: ".clean_string($location)."
";

I have been trying to change the !isset all day and the $symptoms lines to get the 'symptoms' select multiple to show up on my email as an array. Right now it just says "array" in the email. I was thinking maybe the .clean-string should be something to do with array instead of string? I'm completely stuck, I've tried everything I can find, copied and pasted and tinkered and changed about 5 different sets of codes I found on stackoverflow...

</div>
  • 写回答

1条回答 默认 最新

  • duanfu7994 2015-06-17 23:53
    关注

    If you echo (or concatenate, or some other method which will cast to string) an array, it will just say "Array", so you have to convert it to a string somehow. One method is to use implode, so you can display the array as a comma separated list:

    $email_message .= "Symptoms: ".clean_string(implode(", ",$symptoms))."
    ";
    

    http://php.net/implode

    if you want to get fancy and make sure there is an " and" before the last item, you could do

    $symptomString = implode(", ", $symptoms); 
    $lastComma = strrpos($symptomString, ", ");
    $symptomString = substr_replace($symptomString, "and", $lastComma, 2);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)