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 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd