dongzong1866 2016-06-01 22:58
浏览 35
已采纳

如何在一个字段数组中删除空响应?

I've got a PHP script to take answers from a referral form and send it by email to my client (below). The client has now asked that only questions that have been answered be sent to them. How can I strip out the fields that are empty from the email?

 $from = $_REQUEST['Email'] ; 
 $name = $_REQUEST['Name'] ; 
 $headers = "From: $from"; 
 $subject = "Web Contact Data-Medical Referral"; 

 $fields = array(); 
 $fields{"referrer"} = "Referral Source"; 
 $fields{"referraldate"} = "Date of Referral"; 
 $fields{"filenumber"} = "File Number"; 
 $fields{"hearingdate"} = "Hearing Date"; 
 $fields{"legalfirm"} = "Legal Firm"; 
 $fields{"Address"} = "Legal Firm Address";
 $fields{"plantiffattorney"} = "Plantiff Attorney";
 $fields{"plantiffaddress"} = "Plantiff Attorney Address";
 $fields{"plantifftelephone"} = "Plantiff Attorney Telephone";
 $fields{"plantiffemail"} = "Plantiff Attorney Email";
 $fields{"defenceattorney"} = "Defence Attorney";
 $fields{"defenceaddress"} = "Defence Attorney Address";
 $fields{"defencetelephone"} = "Defence Attorney Telephone"; 
 $fields{"defenceemail"} = "Defence Attorney Email";
 $fields{"lawclerk"} = "Legal Assistant/Law Clerk";
 $fields{"lawclerktelephone"} = "Legal Assistant/Law Clerk Telephone";
 $fields{"lawclerkemail"} = "Legal Assistant/Law Clerk Email";
 $fields{"insurancecompany"} = "Name of Insurance Company";
 $fields{"insuranceaddress"} = "Insurance Company Address";
 $fields{"Adjuster"} = "Adjuster Name";
 $fields{"adjusternumber"} = "Adjuster Telephone";
 $fields{"adjusterfax"} = "Adjuster Fax";
 $fields{"adjusteremail"} = "Adjuster Email";
 $fields{"claimnumber"} = "Claim Number";
 $fields{"claimantname"} = "Claimant Name";
 $fields{"gender"} = "Gender";
 $fields{"dateofbirth"} = "Date of Birth";
 $fields{"claimantaddress"} = "Claimant Address";
 $fields{"claimantcity"} = "Claimant City";
 $fields{"claimantpostal"} = "Claimant Postal Code";
 $fields{"claimanttelephone"} = "Claimant Telephone";
 $fields{"lossdate"} = "Date of Loss";
 $fields{"Injuries"} = "Injuries";
 $fields{"Diagnosis"} = "Diagnosis";
 $fields{"reportaddress"} = "Report to Address";
 $fields{"transportation"} = "Transportation Required";
 $fields{"translation"} = "Translation Required";
 $fields{"language"} = "Language";
 $fields{"abilitiesevaluation"} = "Abilities Evaluation";
 $fields{"occupationaltherapist"} = "Legal IME-Occupational Therapist";
 $fields{"rheumatologist"} = "Legal IME-Rheumatologist";
 $fields{"chiropractic"} = "Legal IME-Chiropractic";
 $fields{"opthamologist"} = "Legal IME-Opthamologist";
 $fields{"socialworker"} = "Legal IME-Social Worker";
 $fields{"dental"} = "Legal IME-Dental";
 $fields{"optomertrist"} = "Legal IME-Optometrist";
 $fields{"slp"} = "Legal IME-SLP";
 $fields{"endocrinologist"} = "Legal IME-Endocrinologist";
 $fields{"orthopedic"} = "Legal IME-Orthopedic Surgeon";
 $fields{"tsa"} = "Legal IME-TSA";
 $fields{"ent"} = "Legal IME-ENT";
 $fields{"pediatrician"} = "Legal IME-Pediatrician";
 $fields{"vascularsurgeon"} = "Legal IME-Vascular Surgeon";
 $fields{"gastroenterologist"} = "Legal IME-Gastroenterologist";
 $fields{"physician"} = "Legal IME-Physician";
 $fields{"vocationalevaluation"} = "Legal IME-Vocational Evaluation";
 $fields{"internalmedicine"} = "Legal IME-Internal Medicine";
 $fields{"physiatrist"} = "Legal IME-Physiatrist";
 $fields{"legalfilereview"} = "Legal File Review";
 $fields{"jobanalysis"} = "Legal IME-Job Analysis/WSA";
 $fields{"physiotherapist"} = "Legal IME-Physiotherapist";
 $fields{"lifecare"} = "Life Care Plan";
 $fields{"neurologist"} = "Legal IME-Neurologist";
 $fields{"psychiatrist"} = "Legal IME-Psychiatrist";
 $fields{"lifecarereview"} = "Life Care Plan Review";
 $fields{"neuropsychologist"} = "Legal IME-Neuropsychologist";
 $fields{"psychologist"} = "Legal IME-Psychologist";
 $fields{"other"} = "Other";
 $fields{"otherrequired"} = "Service";
 $fields{"neurosurgeon"} = "Legal IME-Neurosurgeon";
 $fields{"psychovocational"} = "Legal IME-Pyschovocational";
 $fields{"comments"} = "Comments";



 $body="Referral Info:

";


 foreach($fields as $a => $b) {


     $body .= sprintf("%20s:%s
",$b,$_REQUEST[$a]);
 }


if(empty($fields)) {
     echo 'Please fill in the submission form';
     return false;
 }

$send = mail($email, $subject, $body);

if ($send){
    print "Thank you!";

}else{
     print "Oops!";
}
  • 写回答

2条回答 默认 最新

  • duanhuangyun3887 2016-06-01 23:19
    关注

    Check whether the field is empty before adding it to the mail.

    foreach($fields as $a => $b) {
        if (!empty($_REQUEST[$a])) {
            $body .= sprintf("%20s:%s
    ",$b,$_REQUEST[$a]);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器