将额外的变量添加到新的主机php表单

时间:2013-04-18 23:24:48

标签: php forms

我只是想在我的网站上设置一个新的电子邮件表单,我已设法让它从表单中获取电子邮件,评论和名称,并在用户按下提交时将其发送到我指定的电子邮件地址 - 我的问题是我如何添加额外的费用让我们说车辆登记和电话号码?

我假设我会使用相同的属性添加另外两个字段但使用不同的id,然后编辑send-mail.php文件,并在发送邮件中已添加的那两个新变量旁边添加。 php文件,即电子邮件变量和名称变量以及注释变量。

这是我提出的代码,因为现在它仍然可以工作,但我不会得到所有发送到我的电子邮件的信息,它缺少2个新的变量....

  <!-- form -->
                <script type="text/javascript" src="js/form-validation.js"></script>
                <form id="contactForm" action="#" method="post">
                    <h2 class="heading">Contact us using this form</h2>
                    <p>If you have any queries at all or just want to get rid of your car as soon as possible then just leave us the requested details on the form below, press send and a member of our team will get back to you soon.</p>
                    <fieldset>
                        <div>
                            <input name="name"  id="name" type="text" class="form-poshytip" title="Enter your full name" />
                            <label>Name</label>
                        </div>
                        <div>
                            <input name="email"  id="email" type="text" class="form-poshytip" title="Enter your email address" />
                            <label>Email</label>
                        </div>
                        <div>

                            <input name="reg"  id="reg" type="text" class="form-poshytip" title="Enter your vehicle registration" />
                            <label>Vehicle Registration</label>
                        </div>
                        <div>

                            <input name="phone"  id="phone" type="text" class="form-poshytip" title="Enter your phone number" />
                            <label>Phone Number</label>
                        </div>
                        <div>
                            <textarea  name="comments"  id="comments" rows="5" cols="20" class="form-poshytip" title="Enter your comments"></textarea>
                        </div>

                        <!-- send mail configuration -->
                        <input type="hidden" value="enquiries@scrapyourmotor.com" name="to" id="to" />
                        <input type="hidden" value="ScrapYourMotor FORM" name="subject" id="subject" />
                        <input type="hidden" value="send-mail.php" name="sendMailUrl" id="sendMailUrl" />
                        <!-- ENDS send mail configuration -->

                        <p><input type="button" value="Send" name="submit" id="submit" /> <span id="error" class="warning">Message</span></p>
                    </fieldset>

                </form>
                <p id="sent-form-msg" class="success">Form data sent. Thanks for your comments.</p>
                <!-- ENDS form -->

和php文件......

                <?php
                //vars
                $subject = $_POST['subject'];
                $to = explode(',', $_POST['to'] );

                $from = $_POST['email'];

                //data
                $msg = "NAME: "  .$_POST['name']    ."<br>\n";
                $msg .= "EMAIL: "  .$_POST['email']    ."<br>\n";
                $msg .= "REG: "  .$_POST['reg']    ."<br>\n";
                $msg .= "PHONE: "  .$_POST['phone']    ."<br>\n";
                $msg .= "COMMENTS: "  .$_POST['comments']    ."<br>\n";

                //Headers
                $headers  = "MIME-Version: 1.0\r\n";
                $headers .= "Content-type: text/html; charset=UTF-8\r\n";
                $headers .= "From: <".$from. ">" ;


                //send for each mail
                foreach($to as $mail){
                   mail($mail, $subject, $msg, $headers);
                }

                ?>

我哪里错了?这是我在电子邮件中收到的输出:

NAME:我发送的名字

电子邮件:我发送的电子邮件

车辆登记:(这里什么都没有?)

电话号码:(这里什么都没有?)

评论:我发送的评论

感谢任何人的帮助:)

1 个答案:

答案 0 :(得分:0)

嗯我很傻我忘了也用2个新变量更新form-validation.js文件:) ...

        // reg
    var reg = $("input#reg").val();
    if(reg == ""){
        $("#error").fadeIn().text("Reg required");
        $("input#reg").focus();
        return false;
    }

    // comments
    var comments = $("#comments").val();

    // phone
    var phone = $("#phone").val();