Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

php - PHP:mail()-发送多个主题(PHP : mail() - send multiple subject)

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 = '[email protected]';
    $to = '[email protected]';
    $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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are multiple ways.

(有多种方法。)

One is to add a hidden field:

(一种是添加一个隐藏字段:)

In the first form

(第一种形式)

<input type="hidden" name="form_name" value="the_first_form"/>

In the second form

(在第二种形式)

<input type="hidden" name="form_name" value="the_second_form"/>

In PHP then do

(然后在PHP中执行)

if ($_POST["form_name"] === "the_first_form") {
  $subject = "Subject for the first form";
} else {
  $subject = "another subject";
}

Sidenote: Be aware that the browser only submits one form.

(旁注:请注意,浏览器仅提交一种表单。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...