Спасибо! У меня есть форма, которая делает то, что я хочу сейчас! Это заняло немного времени, но я понял, какой код удалить и куда добавить предложенный код.

кая версия php У меня есть форма с двумя частями. Заявитель и соискатель. Я пытаюсь сделать так, чтобы, если поля соискателя были пустыми или не требовалиphp Файл не будет публиковать все пустые поля при отправке, так как я получаю две страницы с заголовками и без ввода. Форма отправлена ​​по электронной почте.

Я только что добавил раздел соискателя, и он находится в скрытомdiv показывает, когда установлен флажок.

Это страница, над которой я работаю.https://sundenmanagement.com/home1/index.php/test Полный код PHP - я пытаюсь все поля с _co в конце представить только при наличии ввода текста. поэтому, если нет соискателя, он не будет печатать все поданные названия.

($ body_message. = 'Имя:'. $ field_first_name_co. "\ r \ n";) эти разделы печатаются как Имя: и я не хочу, чтобы они печатались вообще, если они пустые

<?php
$field_rental_address = $_POST['ra_rental_address'];
$field_date_required = $_POST['ra_date_required'];
$field_first_name = $_POST['ra_first_name'];
$field_middle_name =$_POST['ra_middle_name'];
$field_last_name = $_POST['ra_last_name'];
$field_all_occupants = $_POST['ra_all_occupants'];
$field_num_pets = $_POST['ra_num_pets'];
$field_type_pets = $_POST['ra_pettype'];
$field_cur_phone = $_POST['ra_cur_phone'];
$field_cur_email = $_POST['ra_cur_email']; 
$field_dob = $_POST['ra_dob'];  
$field_sin = $_POST['ra_sin'];
$field_dl_num = $_POST['ra_dl_num'];
$field_dl_issued_prov= $_POST['ra_dl_issued_prov'];
$field_dl_issued_date = $_POST['ra_dl_issued_date'];
$field_dl_expires = $_POST['ra_dl_expires'];
$field_car_one_year =$_POST['ra_car_one_year'];
$field_car_one_make =$_POST['ra_car_one_make'];
$field_car_one_color =$_POST['ra_car_one_color'];
$field_car_one_plate =$_POST['ra_car_one_plate'];
$field_car_one_province =$_POST['ra_car_one_province'];
$field_car_two_year =$_POST['ra_car_two_year'];
$field_car_two_make =$_POST['ra_car_two_make'];
$field_car_two_color =$_POST['ra_car_two_color'];
$field_car_two_plate =$_POST['ra_car_two_plate'];
$field_car_two_province =$_POST['ra_car_two_province'];
$field_ref_one =$_POST['ra_ref_one'];
$field_ref_two =$_POST['ra_ref_two'];
$field_cur_address = $_POST['ra_cur_address'];  
$field_cur_city = $_POST['ra_cur_city']; 
$field_cur_province = $_POST['ra_cur_province'];
$field_cur_postal = $_POST['ra_cur_postal'];
$field_cur_rent = $_POST['ra_cur_rent'];
$field_residing_length = $_POST['ra_cur_residing_length'];  
$field_cur_reason_leaving = $_POST['ra_cur_reason_leaving'];
$field_cur_landlord =$_POST['ra_cur_landlord'];
$field_cur_landlords_phone = $_POST['ra_cur_landlords_phone'];
$field_prev_address = $_POST['ra_prev_address'];
$field_prev_city = $_POST['ra_prev_city'];
$field_prev_province = $_POST['ra_prev_province'];
$field_prev_postal = $_POST['ra_prev_postal'];
$field_prev_length = $_POST['ra_prev_length'];
$field_prev_rent= $_POST['ra_prev_rent'];
$field_prev_reason_leaving = $_POST['ra_prev_reason_leaving'];
$field_prev_landlord = $_POST['ra_prev_landlord'];
$field_prev_landlord_phone = $_POST['ra_prev_landlord_phone'];
$field_cur_employer = $_POST['ra_cur_employer'];
$field_cur_employer_phone = $_POST['ra_cur_employer_phone'];
$field_cur_employer_address = $_POST['ra_cur_employer_address'];
$field_cur_position = $_POST['ra_cur_position'];
$field_cur_job_years = $_POST['ra_cur_job_years'];
$field_cur_salary = $_POST['ra_cur_salary'];
$field_other_income = $_POST['ra_other_income'];
$field_bank_location = $_POST['ra_bank_location'];
$field_prev_employer = $_POST['ra_prev_employer'];
$field_prev_employer_phone = $_POST['ra_prev_employer_phone'];
$field_prev_position = $_POST['ra_prev_position'];
$field_prev_job_years = $_POST['ra_prev_job_years'];
$field_emer_one =$_POST['ra_emer_one'];
$field_emer_one_phone =$_POST['ra_emer_one_phone'];
$field_emer_one_relation =$_POST['ra_emer_one_relation'];
$field_emer_one_address =$_POST['ra_emer_one_address'];
$field_emer_two =$_POST['ra_emer_two'];
$field_emer_two_phone =$_POST['ra_emer_two_phone'];
$field_emer_two_relation =$_POST['ra_emer_two_relation'];
$field_emer_two_address =$_POST['ra_emer_two_address'];
foreach($_POST['check'] as $value) {
    $check_boxes .= $value." ";
}
$drop_down_item = $_POST['cf_drop_down'];
$field_first_name_co = $_POST['ra_first_name_co'];
$field_middle_name_co =$_POST['ra_middle_name_co'];
$field_last_name_co = $_POST['ra_last_name_co'];
$field_all_occupants_co = $_POST['ra_all_occupants_co'];
$field_num_pets_co = $_POST['ra_num_pets_co'];
$field_type_pets_co = $_POST['ra_pettype_co'];
$field_cur_phone_co = $_POST['ra_cur_phone_co'];
$field_cur_email_co = $_POST['ra_cur_email_co']; 
$field_dob_co = $_POST['ra_dob_co'];    
$field_sin_co = $_POST['ra_sin_co'];
$field_dl_num_co = $_POST['ra_dl_num_co'];
$field_dl_issued_prov_co= $_POST['ra_dl_issued_prov_co'];
$field_dl_issued_date_co = $_POST['ra_dl_issued_date_co'];
$field_dl_expires_co = $_POST['ra_dl_expires_co'];
$field_car_one_year_co =$_POST['ra_car_one_year_co'];
$field_car_one_make_co =$_POST['ra_car_one_make_co'];
$field_car_one_color_co =$_POST['ra_car_one_color_co'];
$field_car_one_plate_co =$_POST['ra_car_one_plate_co'];
$field_car_one_province_co =$_POST['ra_car_one_province_co'];
$field_car_two_year_co =$_POST['ra_car_two_year_co'];
$field_car_two_make_co =$_POST['ra_car_two_make_co'];
$field_car_two_color_co =$_POST['ra_car_two_color_co'];
$field_car_two_plate_co =$_POST['ra_car_two_plate_co'];
$field_car_two_province_co =$_POST['ra_car_two_province_co'];
$field_ref_one_co =$_POST['ra_ref_one_co'];
$field_ref_two_co =$_POST['ra_ref_two_co'];
$field_cur_address_co = $_POST['ra_cur_address_co'];    
$field_cur_city_co = $_POST['ra_cur_city_co']; 
$field_cur_province_co = $_POST['ra_cur_province_co'];
$field_cur_postal_co = $_POST['ra_cur_postal_co'];
$field_cur_rent_co = $_POST['ra_cur_rent_co'];
$field_residing_length_co = $_POST['ra_cur_residing_length_co'];    
$field_cur_reason_leaving_co = $_POST['ra_cur_reason_leaving_co'];
$field_cur_landlord_co =$_POST['ra_cur_landlord_co'];
$field_cur_landlords_phone_co = $_POST['ra_cur_landlords_phone_co'];
$field_prev_address_co = $_POST['ra_prev_address_co'];
$field_prev_city_co = $_POST['ra_prev_city_co'];
$field_prev_province_co = $_POST['ra_prev_province_co'];
$field_prev_postal_co = $_POST['ra_prev_postal_co'];
$field_prev_length_co = $_POST['ra_prev_length_co'];
$field_prev_rent_co= $_POST['ra_prev_rent_co'];
$field_prev_reason_leaving_co = $_POST['ra_prev_reason_leaving_co'];
$field_prev_landlord_co = $_POST['ra_prev_landlord_co'];
$field_prev_landlord_phone_co = $_POST['ra_prev_landlord_phone_co'];
$field_cur_employer_co = $_POST['ra_cur_employer_co'];
$field_cur_employer_phone_co = $_POST['ra_cur_employer_phone_co'];
$field_cur_employer_address_co = $_POST['ra_cur_employer_address_co'];
$field_cur_position_co = $_POST['ra_cur_position_co'];
$field_cur_job_years_co = $_POST['ra_cur_job_years_co'];
$field_cur_salary_co = $_POST['ra_cur_salary_co'];
$field_other_income_co = $_POST['ra_other_income_co'];
$field_bank_location_co = $_POST['ra_bank_location_co'];
$field_prev_employer_co = $_POST['ra_prev_employer_co'];
$field_prev_employer_phone_co = $_POST['ra_prev_employer_phone_co'];
$field_prev_position_co = $_POST['ra_prev_position_co'];
$field_prev_job_years_co = $_POST['ra_prev_job_years_co'];
$field_emer_one_co =$_POST['ra_emer_one_co'];
$field_em,er_one_phone_co =$_POST['ra_emer_one_phone_co'];
$field_emer_one_relation_co =$_POST['ra_emer_one_relation_co'];
$field_emer_one_address_co =$_POST['ra_emer_one_address_co'];
$field_emer_two_co =$_POST['ra_emer_two_co'];
$field_emer_two_phone_co =$_POST['ra_emer_two_phone_co'];
$field_emer_two_relation_co =$_POST['ra_emer_two_relation_co'];
$field_emer_two_address_co =$_POST['ra_emer_two_address_co'];
foreach($_POST['check_co'] as $value) {
    $check_boxes .= $value." ";
}
$drop_down_item_co = $_POST['cf_drop_down_co'];
$mail_to = '[email protected]';
$subject = 'Rental Application '.$field_first_name;
$body_message = 'Rental Address: '.$field_rental_address."\r\n";
$body_message .= 'Date Required: '.$field_date_required."\r\n";
$body_message .= 'First Name: '.$field_first_name."\r\n";
$body_message .= 'Middle Name: '.$field_middle_name."\r\n";
$body_message .= 'Last Name: '.$field_last_name."\r\n";
$body_message .= 'List All occupants: '.$field_all_occupants."\r\n";
$body_message .= 'Number of Pets: '.$field_num_pets."\r\n";
$body_message .= 'Type of Pets : '.$field_type_pets."\r\n";
$body_message .= 'Phone: '.$field_cur_phone."\r\n";
$body_message .= 'E-mail: '.$field_cur_email."\r\n";
$body_message .= 'Date of Birth: '.$field_dob."\r\n";
$body_message .= 'SIN: '.$field_sin."\r\n";
$body_message .= 'Drivers License Number: '.$field_dl_num."\r\n";
$body_message .= 'Date Issued : '.$field_dl_issued_date."\r\n";
$body_message .= 'Drives License Issued: '.$field_dl_issued_prov."\r\n";
$body_message .= 'Drives License Expires: '.$field_dl_expires."\r\n";
$body_message .= 'Vehicle #1 Year: '.$field_car_one_year."\r\n";
$body_message .= 'Vehicle #1 Make: '.$field_car_one_make."\r\n";
$body_message .= 'Vehicle #1 Color: '.$field_car_one_color."\r\n";
$body_message .= 'Vehicle #1 Plate: '.$field_car_one_plate."\r\n";
$body_message .= 'Vehicle #1 Province Issued: '.$field_car_one_province."\r\n";
$body_message .= 'Vehicle #2 Year: '.$field_car_two_year."\r\n";
$body_message .= 'Vehicle #2 Make: '.$field_car_two_make."\r\n";
$body_message .= 'Vehicle #2 Color: '.$field_car_two_color."\r\n";
$body_message .= 'Vehicle #2 Plate: '.$field_car_two_plate."\r\n";
$body_message .= 'Vehicle #2 Province Issued: '.$field_car_two_province."\r\n";
$body_message .= 'Credit Reference 1: '.$field_ref_one."\r\n";
$body_message .= 'Credit Reference 2: '.$field_ref_two."\r\n";
$body_message .= 'Current Address: '.$field_cur_address."\r\n";
$body_message .= 'City: '.$field_cur_city."\r\n";
$body_message .= 'Province: '.$field_cur_province."\r\n";
$body_message .= 'Postal Code: '.$field_cur_postal."\r\n";
$body_message .= 'residing at this address: '.$field_residing_length."\r\n";
$body_message .= 'Rent Amount   : '.$field_cur_rent."\r\n";
$body_message .= 'Reason for Leaving: '.$field_cur_reason_leaving."\r\n";
$body_message .= 'Current Landlords Name: '.$field_cur_landlord."\r\n";
$body_message .= 'Current Landlords Phone: '.$field_cur_landlords_phone."\r\n";
$body_message .= 'Previous Address: '.$field_prev_address."\r\n";
$body_message .= 'City: '.$field_prev_city."\r\n";
$body_message .= 'Province: '.$field_prev_province."\r\n";
$body_message .= 'Postal Code: '.$field_prev_postal."\r\n";
$body_message .= 'residing at this address: '.$field_prev_length."\r\n";
$body_message .= 'Rent Amount   : '.$field_prev_rent."\r\n";
$body_message .= 'Reason for Leaving: '.$field_prev_reason_leaving."\r\n";
$body_message .= 'Previous Landlords Name: '.$field_prev_landlord."\r\n";
$body_message .= 'Previous Landlords Phone: '.$field_prev_landlord_phone."\r\n";
$body_message .= 'Current Employer: '.$field_cur_employer."\r\n";
$body_message .= 'Current Employers Phone: '.$field_cur_employer_phone."\r\n";
$body_message .= 'Current Employers Address: '.$field_cur_employer_address."\r\n";
$body_message .= 'Job Position: '.$field_cur_position."\r\n";
$body_message .= 'Years Employed: '.$field_cur_job_years."\r\n";
$body_message .= 'Salary: '.$field_cur_salary."\r\n";
$body_message .= 'Other Income: '.$field_other_income."\r\n";
$body_message .= 'Bank(Branch Location): '.$field_bank_location."\r\n";
$body_message .= 'Previous Employer: '.$field_prev_employer."\r\n";
$body_message .= 'Previous Employers Phone: '.$field_prev_employer_phone."\r\n";
$body_message .= 'Job Position: '.$field_prev_position."\r\n";
$body_message .= 'Years Employed: '.$field_prev_job_years."\r\n";
$body_message .= 'Emergency Contact #1: '.$field_emer_one."\r\n";
$body_message .= 'Phone Number: '.$field_emer_one_phone."\r\n";
$body_message .= 'Relationship: '.$field_emer_one_relation."\r\n";
$body_message .= 'Address: '.$field_emer_one_address."\r\n";
$body_message .= 'Emergency Contact #2: '.$field_emer_two."\r\n";
$body_message .= 'Phone Number: '.$field_emer_two_phone."\r\n";
$body_message .= 'Relationship: '.$field_emer_two_relation."\r\n";
$body_message .= 'Address: '.$field_emer_two_address."\r\n";
$body_message .= "How did you hear about us?: ".$drop_down_item."\r\n";
$body_message .= 'I allow Sunden Management to use the information within this form to preform a credit check, employment reference check, landlord reference check and any other inquiries deemed necessary to secure a rental property.: '.$check_boxes."\r\n";
$body_message .= 'First Name: '.$field_first_name_co."\r\n";
$body_message .= 'Last Name: '.$field_last_name_co."\r\n";
$body_message .= 'First Name: '.$field_first_name_co."\r\n";
$body_message .= 'Middle Name: '.$field_middle_name_co."\r\n";
$body_message .= 'Last Name: '.$field_last_name_co."\r\n";
$body_message .= 'List All occupants: '.$field_all_occupants_co."\r\n";
$body_message .= 'Number of Pets: '.$field_num_pets_co."\r\n";
$body_message .= 'Type of Pets : '.$field_type_pets_co."\r\n";
$body_message .= 'Phone: '.$field_cur_phone_co."\r\n";
$body_message .= 'E-mail: '.$field_cur_email_co."\r\n";
$body_message .= 'Date of Birth: '.$field_dob_co."\r\n";
$body_message .= 'SIN: '.$field_sin_co."\r\n";
$body_message .= 'Drivers License Number: '.$field_dl_num_co."\r\n";
$body_message .= 'Date Issued : '.$field_dl_issued_date_co."\r\n";
$body_message .= 'Drives License Issued: '.$field_dl_issued_prov_co."\r\n";
$body_message .= 'Drives License Expires: '.$field_dl_expires_co."\r\n";
$body_message .= 'Vehicle #1 Year: '.$field_car_one_year_co."\r\n";
$body_message .= 'Vehicle #1 Make: '.$field_car_one_make_co."\r\n";
$body_message .= 'Vehicle #1 Color: '.$field_car_one_color_co."\r\n";
$body_message .= 'Vehicle #1 Plate: '.$field_car_one_plate_co."\r\n";
$body_message .= 'Vehicle #1 Province Issued: '.$field_car_one_province_co."\r\n";
$body_message .= 'Vehicle #2 Year: '.$field_car_two_year_co."\r\n";
$body_message .= 'Vehicle #2 Make: '.$field_car_two_make_co."\r\n";
$body_message .= 'Vehicle #2 Color: '.$field_car_two_color_co."\r\n";
$body_message .= 'Vehicle #2 Plate: '.$field_car_two_plate_co."\r\n";
$body_message .= 'Vehicle #2 Province Issued: '.$field_car_two_province_co."\r\n";
$body_message .= 'Credit Reference 1: '.$field_ref_one_co."\r\n";
$body_message .= 'Credit Reference 2: '.$field_ref_two_co."\r\n";
$body_message .= 'Current Address: '.$field_cur_address_co."\r\n";
$body_message .= 'City: '.$field_cur_city_co."\r\n";
$body_message .= 'Province: '.$field_cur_province_co."\r\n";
$body_message .= 'Postal Code: '.$field_cur_postal_co."\r\n";
$body_message .= 'residing at this address: '.$field_residing_length_co."\r\n";
$body_message .= 'Rent Amount   : '.$field_cur_rent_co."\r\n";
$body_message .= 'Reason for Leaving: '.$field_cur_reason_leaving_co."\r\n";
$body_message .= 'Current Landlords Name: '.$field_cur_landlord_co."\r\n";
$body_message .= 'Current Landlords Phone: '.$field_cur_landlords_phone_co."\r\n";
$body_message .= 'Previous Address: '.$field_prev_address_co."\r\n";
$body_message .= 'City: '.$field_prev_city_co."\r\n";
$body_message .= 'Province: '.$field_prev_province_co."\r\n";
$body_message .= 'Postal Code: '.$field_prev_postal_co."\r\n";
$body_message .= 'residing at this address: '.$field_prev_length_co."\r\n";
$body_message .= 'Rent Amount   : '.$field_prev_rent_co."\r\n";
$body_message .= 'Reason for Leaving: '.$field_prev_reason_leaving_co."\r\n";
$body_message .= 'Previous Landlords Name: '.$field_prev_landlord_co."\r\n";
$body_message .= 'Previous Landlords Phone: '.$field_prev_landlord_phone_co."\r\n";
$body_message .= 'Current Employer: '.$field_cur_employer_co."\r\n";
$body_message .= 'Current Employers Phone: '.$field_cur_employer_phone_co."\r\n";
$body_message .= 'Current Employers Address: '.$field_cur_employer_address_co."\r\n";
$body_message .= 'Job Position: '.$field_cur_position_co."\r\n";
$body_message .= 'Years Employed: '.$field_cur_job_years_co."\r\n";
$body_message .= 'Salary: '.$field_cur_salary_co."\r\n";
$body_message .= 'Other Income: '.$field_other_income_co."\r\n";
$body_message .= 'Bank(Branch Location): '.$field_bank_location_co."\r\n";
$body_message .= 'Previous Employer: '.$field_prev_employer_co."\r\n";
$body_message .= 'Previous Employers Phone: '.$field_prev_employer_phone_co."\r\n";
$body_message .= 'Job Position: '.$field_prev_position_co."\r\n";
$body_message .= 'Years Employed: '.$field_prev_job_years_co."\r\n";
$body_message .= 'Emergency Contact #1: '.$field_emer_one_co."\r\n";
$body_message .= 'Phone Number: '.$field_emer_one_phone_co."\r\n";
$body_message .= 'Relationship: '.$field_emer_one_relation_co."\r\n";
$body_message .= 'Address: '.$field_emer_one_address_co."\r\n";
$body_message .= 'Emergency Contact #2: '.$field_emer_two_co."\r\n";
$body_message .= 'Phone Number: '.$field_emer_two_phone_co."\r\n";
$body_message .= 'Relationship: '.$field_emer_two_relation_co."\r\n";
$body_message .= 'Address: '.$field_emer_two_address_co."\r\n";
$body_message .= "How did you hear about us?: ".$drop_down_item_co."\r\n";
$body_message .= 'I allow Sunden Management to use the information within this form to preform a credit check, employment reference check, landlord reference check and any other inquiries deemed necessary to secure a rental property.: '.$check_boxes."\r\n";
$body_message .= 'First Name: '.$field_first_name_co."\r\n";
$body_message .= 'Last Name: '.$field_last_name_co."\r\n";
$headers = 'From: '.$field_cur_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for your application. We will contact you shortly.');
        window.location = 'home1/index.php';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to [email protected]');
        window.location = 'home1/index.php';
    </script>
<?php
}
 Barmar21 сент. 2017 г., 23:45
Поля, которые скрыты, обычно не отправляются как часть формы.
 chris8522 сент. 2017 г., 01:04
Удалите пустые строки и вставьте код в вопрос.
 user801199721 сент. 2017 г., 23:41
if (!empty($field)){...}
 Magnus Eriksson21 сент. 2017 г., 23:41
Так как дела? Возникли проблемы? Если да, пожалуйста, уточните и покажите нам код.
 Jason22 сент. 2017 г., 00:58
Я добавил ссылку на страницу и короткую версию php на свой вопрос. Я удалил большинство предметов, но оставил основные части, так как это было слишком долго. Спасибо за любую помощь в этом! Новое в php.

Ответы на вопрос(1)

Решение Вопроса

если немного переделать имена полей формы. Например, эта строка в вашем скрипте:

$body_message .= 'Reason for Leaving: '.$field_cur_reason_leaving."\r\n";

Если вы измените это имя поля формы на$reason_for_leaving в своем HTML - и сделайте то же самое для других полей - вы можете обработать свою форму и сгенерировать свое электронное сообщение следующим образом:

$body_message = ''; // initialize email body

foreach ( $_POST as $key => $value ) {
    if ($key == 'somethingspecial') {
        // special parsing for this field
    } else {
        // some basic sanitization
        $_POST[$key] = trim(stripcslashes(strip_tags( $value )));
        if ($_POST[$key] != '') {
            // If field isn't blank, make the form field name
            // look nice and add the value for the form field
            $body_message .= ucwords(str_ireplace( '_', ' ', $key )) .': '. $value . "\r\n";
        }
    }
}

Очевидно, вам нужно будет немного больше поработать со специальными полями формы (такими как флажки), но это должно значительно упростить ваш код (и вашу жизнь).

 Jason13 окт. 2017 г., 20:35
Спасибо! У меня есть форма, которая делает то, что я хочу сейчас! Это заняло немного времени, но я понял, какой код удалить и куда добавить предложенный код.
 Jason22 сент. 2017 г., 23:00
Спасибо, смекош, я попробую. Мой код отлично работал для простой контактной формы, но слишком много для приложения. Все мои php-навыки были самоучками.

Ваш ответ на вопрос