表单字段值未传递给PHP文件

时间:2016-09-18 03:11:40

标签: php jquery html ajax phpmailer

所以即时制作一个电子邮件表单,并使用AJAX链接到使用PHPMailer发送电子邮件的外部PHP文件。所以我在PHP的顶部有这个代码:

if(count($_POST) > 0){

    $message=
    'Full Name: '.$_POST['name'].'<br />
    Email:  '.$_POST['email'].'<br />
    Message: '.$_POST['textarea'].'<br />
    Current Website: '.$_POST['website'].'<br />
    Business Name: '.$_POST['business'].'<br />
    ';

  • 在使用wamp的localhost中,这样可以正常工作并发送带有相应字段的电子邮件
  • 在实时版本中,电子邮件未通过PHP文件中的if(count($_POST) > 0)
  • 如果if语句被删除,它会发送电子邮件,我确实收到了,但电子邮件只有&#34;全名:&#34;和&#34;电子邮件:&#34;等等,填充空格,没有从HTML表单输入的值。这是AJAX代码:

    var ajax = {
        isSubmiting: false,
        send: function() {
    
            if(ajax.isSubmiting == false) {
                ajax.isSubmiting = true;
    
                var name = $("input[name=name]").val();
                var email = $("input[name=email]").val();
                var textarea = $("textarea").val();
                var business = $("input[name=business]").val();
                var website = $("input[name=website]").val();
    
                    ajax.SetText("Sending...");
                    $.post("sendmail.php", {
                        name: name, email: email, textarea: textarea, business: business, website: website
                    }, function(data) {
                        if (data.trim() == "true") {
    
                            ajax.SetText("Sent!");  
                            console.log(data);
    
    
                            $.get("sent.html", function(sentData){
    
                                $("#content").html(sentData);
    
                            });
    
                        } else {
                            ajax.SetText("Send mail");
                            console.log(data);
                            $.get("unsent.html", function(sentData){
    
                                $("#content").html(sentData);
    
                            });
    
    
                        }
    
                        ajax.isSubmiting = false;
                    });
    
            }
            else alert("You can only send one email at a time!");
        },
        SetText: function(text) {
            $("input[type=button]").val(text);
        }
    }
    

这是表单HTML标记:

<form name="contact-form" method="post" id="contact-form">

                        <h3> We want to hear from you! Tell us about your business and/or project and what you need and we'll get back to you as soon as possible! </h3>

                            <div class="form-group">

                                <label for="name">Your Name:</label>
                                <input type="text" name="name" class="form-control" placeholder="Your Name" required/>

                            </div>

                            <div class="form-group">

                                <label for="email">Your Email:</label>
                                <input type="email" name="email" class="form-control" placeholder="Your Name" required/>

                            </div>
                            <div class="form-group">

                                <label for="web">Current Website (if available):</label>
                                <input type="text" name="website" class="form-control" placeholder="Current Website (if available)"/>

                            </div>

                            <div class="form-group">

                                <label for="web">Business Name (if available):</label>
                                <input type="text" name="business" class="form-control" placeholder="Business Name (if available)"/>

                            </div>

                            <div class="form-group">

                                <label for="comment">Message:</label>
                                <textarea id="textarea" rows="5" class="form-control" name="textarea" placeholder="Message"></textarea>

                            </div>

                            <div class="row" align="center" id="button-row2">

                                <input type="button" value="Submit" onClick="ajax.send()" class="btn">

                            </div>

                    </form>

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

在您的实时环境中无法正确访问jQuery。因此,

var name = $("input[name=name]").val();
var email = $("input[name=email]").val();
var textarea = $("textarea").val();
var business = $("input[name=business]").val();
var website = $("input[name=website]").val();

都是undefined