I have 2 contact forms on one page.
(我在一页上有2个联系表。)
But I want to send different topics with php mail functions
. (但是我想用php mail functions
发送不同的主题。)
When sending data from the first form, "Subject-1" is displayed, If in another form, "Subject-2" is displayed. (从第一种形式发送数据时,显示“ Subject-1”;如果以另一种形式,则显示“ Subject-2”。)
How can I compare each form in a post request to set the correct subject (如何比较邮寄请求中的每个表格以设置正确的主题)
First Form
(第一种形式)
<form id="contact-form" method="POST" class="d-flex form footer-form">
<input type="text" class="simple-input" id="name" name="name" placeholder="Name">
<input type="text" class="simple-input" id="email" name="email" placeholder="Email address">
<textarea class="quession-input" id="msg" name="msg" placeholder="Your question"></textarea>
<div class="checkboks custom-sq">
<input type="checkbox" class="checked-checkbox" name="myCheckboxes" id="box1" checked="checked" value="true" />
<label for="box1" class="checkboks-text"><?php echo the_field('checkbox_text', 'option'); ?></label>
</div>
<button type="submit" class="danger-btn submit"><?php echo the_field('btn_send', 'option'); ?></button>
</form>
Second Form
(第二形式)
<form id="hy-form" method="POST" class="d-flex form">
<input type="text" class="simple-input" name="hy_name" id="hy_name" placeholder="Name">
<input type="text" class="simple-input" name="hy_email" id="hy_email" placeholder="Email address">
<textarea class="quession-input" id="hy_msg" name="hy_msg" placeholder="Your question"></textarea>
<div class="checkboks custom-sq">
<input type="checkbox" id="box3" name="myCheckboxesHy" class="checked-checkbox" checked="checked" value="true"/>
<label for="box3" class="checkboks-text"><?php echo the_field('checkbox_text', 'option'); ?></label>
</div>
<button type="submit" class="danger-btn hy-submit"><?php echo the_field('btn_send', 'option'); ?></button>
</form>
PHP
(的PHP)
add_action('wp_ajax_nopriv_send_email', 'send_email');
add_action('wp_ajax_send_email', 'send_email');
function send_email() {
$checkbox = $_POST['myCheckboxes'];
if (isset($checkbox)) {
echo $checkbox;
}
$checkbox_hy = $_POST['myCheckboxesHy'];
if (isset($checkbox_hy)) {
echo $checkbox_hy;
}
$headers = 'Content-Type: text/html; charset="utf-8"';
$name = $_POST['name'];
$url = $_POST['url'];
$hy_name = $_POST['hy_name'];
$from = 'gtest@gtest.com';
$to = 'gtest@gtest.com';
$email = $_POST['email'];
$hy_email = $_POST['hy_email'];
$msg = $_POST['msg'];
$hy_msg = $_POST['hy_msg'];
$subject = 'Footer form: ' . $_POST['email'];
$message .= (!empty($name)) ? '<p><strong>User Name</strong> : ' . $name .' </p>' : '';
$message .= (!empty($email)) ? '<p><strong>User Email</strong> : '. $email .'</p>' : '';
$message .= (!empty($msg)) ? '<p><strong>User Message</strong> : '.$msg .'</p>' : '';
$message .= (!empty($checkbox)) ? '<p><strong>Checkboxs</strong> : '.$checkbox .'</p>' : '';
$message .= (!empty($hy_name)) ? '<p><strong>User Name</strong> : '.$hy_name .'</p>' : '';
$message .= (!empty($hy_email)) ? '<p><strong>User Email</strong> : '.$hy_email .'</p>' : '';
$message .= (!empty($hy_msg)) ? '<p><strong>User Message</strong> : '.$hy_msg .'</p>' : '';
$message .= (!empty($checkbox_hy)) ? '<p><strong>Checkboxses</strong> : '.$checkbox_hy .'</p>' : '';
$message .= (!empty($url)) ? '<p><strong>Url:</strong> : '.$url .'</p>' : '';
$message .= '</body></html>';
echo mail($to, $subject, $message, $headers);
return $msg;
die();
}
UPDATE:
(更新:)
I try this code, it's work, but right way ?
(我尝试这段代码,它起作用了,但是正确的方法吗?)
if ($hy_name) {
$subject = 'HY form: ' . $_POST['email'];
} elseif ($name) {
$subject = 'Footer form: ' . $_POST['email'];
}
ask by Bavarius translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…