如何在php联系表单中附加选择列表 [英] How to append select list in a php contact form

查看:89
本文介绍了如何在php联系表单中附加选择列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在表单上附加所选的html项目以与电子邮件一起发送。我想知道如何从选择列表中正确获取值并将其传递给PHP。我无法正确弄明白。我将不胜感激任何帮助。下面是我的代码......

I am trying to append the selected html item on the form to be sent along with the email. I would like to know how to correctly fetch the value from the select list and pass it to PHP. I have not been able to correctly figure it out. I would appreciate any assistance. Below is my code...

    function _(id){ return document.getElementById(id); }
function submitForm(){
    _("contact-submit").disabled = true;
    _("status").innerHTML = 'please wait ...';
    var formdata = new FormData();
    formdata.append( "n", _("n").value );
    formdata.append( "e", _("e").value );
    formdata.append( "t", _("t").value );
    formdata.append( "w", _("w").value );
    formdata.append( "m", _("m").value );

    var ajax = new XMLHttpRequest();
    ajax.open( "POST", "contact.php" );
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            if(ajax.responseText == "success"){
                _("contact-form").innerHTML = '<h2 class="tnx">Thanks '+_("n").value+', we will be in touch soon!</h2>';
            } else {
                _("status").innerHTML = ajax.responseText;
                _("contact-submit").disabled = false;
            }
        }
    }
    ajax.send( formdata );
}

HTML

    <div class="cat-select">
                    <label class="cat-selects" for="subject"><span>Area of Interest</span>
                            <select name="subject" class="select-field" id="s">
                            <option value="General Inquiry">General Inquiry</option>
                            <option value="Elderly Care">Elderly Care</option>
                            <option value="Child Care">Child Care</option>
                            </select>
                        </label>
                </div>

PHP

     <?php
    if( isset($_POST['n']) && isset($_POST['e']) && isset($_POST['t']) && isset($_POST['w']) && isset($_POST['m']) ){
        $n = $_POST['n']; // HINT: use preg_replace() to filter the data
        $e = $_POST['e'];
        $t = $_POST['t'];
        $w = $_POST['w'];

        $m = nl2br($_POST['m']);
        $to = "hello@gmail.com";    
        $from = $e;
        $subject = 'Contact Form Message';
        $message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p>'.$s.' '.$m.'</p>';
        $headers = "From: $from\n";
        $headers .= "MIME-Version: 1.0\n";
        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
        if( mail($to, $subject, $message, $headers) ){
            echo "success";
        } else {
            echo "The server failed to send the message. Please try again later.";
        }
    }
    ?>


推荐答案

id < select> 元素的c $ c>是s,但您的Javascript并未在任何地方引用它。假设你的 _()函数引用 id ,你需要添加以下类似行的行。

The id of your <select> element is "s" but your Javascript isn't referencing it anywhere. Assuming your _() function refers to the id, you will need to add the following line with the similar lines.

formdata.append( "s", _("s").value );

这应该是 $ _ POST ['s'] 可以在PHP端使用。

This should make $_POST['s'] available for you to use on the PHP side.

请记住做一些事情来逃避所有通过此表单发送的字段,而不仅仅是文本输入和textareas,以避免任何尝试利用该网站或电子邮件的收件人。

Remember to do something to escape all the fields sent through this form, not just the text inputs and textareas, to avoid any attempts to exploit the site or the recipient of the email.

这篇关于如何在php联系表单中附加选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆