php联系表单返回字段名称而不是值

时间:2014-07-28 18:46:20

标签: javascript php html ajax forms

我是新手,所以请善待。我有一个网站的联系表格。我不确定我做了什么错误。 这是我通过联系表单收到的电子邮件:

<p>$usersname has contacted you from your site.</p><h3>Their Message is as follows:</h3><p>$usersmessage</p>$usersmessage"$usersmessage"<h5>Contact details</h5><p>Phone Number: $usersphonenumber</p><p>Email Address: $usersemail</p>

不确定我做错了什么

它使用这个php文件:

<?php

if(isset($_REQUEST['contactformid']) && $_REQUEST['contactformid'] == 1){

$youremail = "webmaster@trwebcreations.com"; // Enter your email here!!

$usersname = ($_POST["usersname"]);
$usersemail = ($_POST["usersemail"]);
$usersphonenumber = ($_POST["usersphonenumber"];)
$usersmessage = ($_POST["usersmessage"];)
$subject = 'Message From Chris Gent | Landscape Studio website';
$message = '%usersname% has contacted you from your site.
            Their Message is as follows: %usersmessage%

            Contact details
            Phone Number: %usersphonenumber%
            Email Address: %usersemail%; 

$headers = 'From:' . $usersemail . "\r\n";
mail($youremail, $subject, $message, $headers);

echo '<p>Thank you for your email, a member of our staff will contact you</p>';

} else {    echo '<p>Form could not be sent, please try again!</p>';    }

这是html页面上的标题脚本:

<script>$(document).ready(function() {$('#ajaxcontactform').submit(function(){$('input[type=submit]', this).attr('disabled', 'disabled');});



$('#ajaxcontactform').submit(

    function parseResponse() {

        var usersname = $("#name");
        var usersemail = $("#email");
        var usersphonenumber = $("#phone");
        var usersmessage = $("#comment");
        var contactformid = $("#contactformid");
        var url = "contact.php";

            var emailReg = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
            var valid = emailReg.test(usersemail.val());

            if(!valid) {
                $("#contactwarning").html('<p class="rejectionalert"><span>Your email is not valid!</span></p>').slideDown().delay(2000).slideUp();
                $('input[type=submit]', $("#ajaxcontactform")).removeAttr('disabled');
                return false;
            }

          if (usersname.val() == "" || usersname.val() == "Please Insert Your Name") {                
               $("#contactwarning").html('<p class="rejectionalert"><span>Please Insert Your Name!</span></p>').slideDown().delay(2000).slideUp();
               $('input[type=submit]', $("#ajaxcontactform")).removeAttr('disabled');
               return false;               
          }
          if (usersemail.val() == "" || usersemail.val() == "Please Insert Your Email") {
               $("#contactwarning").html('<p class="rejectionalert"><span>Please Insert Your Email!</span></p>').slideDown().delay(2000).slideUp();
               $('input[type=submit]', $("#ajaxcontactform")).removeAttr('disabled');
               return false;
          }
          if (usersphonenumber.val() == "" || usersphonenumber.val() == "Please Insert Your Phone Number") {
               $("#contactwarning").html('<p class="rejectionalert"><span>Please Insert Your Phone Number!</span></p>').slideDown().delay(2000).slideUp();
               $('input[type=submit]', $("#ajaxcontactform")).removeAttr('disabled');
               return false;
          }
          if (usersmessage.val() == "" || usersmessage.val() == "Please Leave A Message") {
               $("#contactwarning").html('<p class="rejectionalert"><span>You forgot to leave a message!</span></p>').slideDown().delay(2000).slideUp();
               $('input[type=submit]', $("#ajaxcontactform")).removeAttr('disabled');
               return false;
          }

                $.post(url,{ usersname: usersname.val(), usersemail: usersemail.val(), usersphonenumber: usersphonenumber.val(), usersmessage: usersmessage.val(), contactformid: contactformid.val() } , function(data) {
                    $('#contactajax').html(data);
                    $('#contactajax').slideDown().delay(1000).slideUp();
                    $("#name").val('Please Insert Your Name');
                    $("#email").val('Please Insert Your Email');
                    $("#phone").val('Please Insert Your Phone Number');
                    $("#comment").val('Please Leave A Message');
                    $('input[type=submit]', $("#ajaxcontactform")).removeAttr('disabled');
                });

      }

  ); });

以下是实际的html表单:

<form action="javascript:parseResponse();" method="post" name="ajaxcontactform" id="ajaxcontactform">

            <div class="contacttextarea">
                <input name="contactformid" id="contactformid" type="hidden" value="1" />

                <fieldset>
                    <textarea name="comment" id="comment" cols="5" rows="5" class="contacttextarea"onfocus="if (this.value == 'Please Leave A Message') {this.value = '';}">Please Leave A Message</textarea>
                </fieldset>

            </div>

            <div class="contacttextboxes">

                <fieldset>
                    <input id="name" name="name" type="text" class="contacttextform" onfocus="if (this.value == 'Please Insert Your Name') {this.value = '';}"value="Please Insert Your Name">
                </fieldset>

                <fieldset>
                    <input id="phone" name="phone" type="text" class="contacttextform" onfocus="if (this.value == 'Please Insert Your Phone Number') {this.value = '';}"value="Please Insert Your Phone Number">
                </fieldset>

                <fieldset>
                    <input id="email" name="email" type="text" class="contacttextform" onfocus="if (this.value == 'Please Insert Your Email') {this.value = '';}"value="Please Insert Your Email">
                </fieldset>

                <fieldset>
                    <input name="send" type="submit" class="contactformbutton" value="Send">
                </fieldset>

            </div>

        </form>

以下是表单验证:

jQuery(document).ready(function($){

// hide messages 
$("#error").hide();
$("#sent-form-msg").hide();

// on submit...
$("#contactForm #submit").click(function() {
    $("#error").hide();

    //required:

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

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

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

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

    // send mail php
    var sendMailUrl = $("#sendMailUrl").val();

    //to, from & subject
    var to = $("#to").val();
    var from = $("#from").val();
    var subject = $("#subject").val();

    // data string
    var dataString = 'name='+ name
                    + '&email=' + email        
                    + '&web=' + web
                    + '&comments=' + comments
                    + '&to=' + to
                    + '&from=' + from
                    + '&subject=' + subject;                                 
    // ajax
    $.ajax({
        type:"POST",
        url: sendMailUrl,
        data: dataString,
        success: success()
    });
});  


// on success...
 function success(){
    $("#sent-form-msg").fadeIn();
    $("#contactForm").fadeOut();
 }

return false;});

2 个答案:

答案 0 :(得分:3)

基本PHP:

$message = '%usersname% has contacted you from your site.';
            ^^^^^^^^^^^----this

您的%whatever%“变量”对PHP有绝对的 NO 含义。如果您希望PHP用值替换变量,则必须使用正确的语法:

$usersname = $_POST["usersname"];    

$mail = "Hello, $usersname, .....";
                ^^^^^^^^^^---a proper PHP variable

请注意,您必须使用" - 引用的字符串。单引号变量中的$foo只有四个字符,$foo。在双引号字符串中,它是一个名为foo的变量,将替换为变量的值。

答案 1 :(得分:0)

原因,您使用的是电子邮件分段格式的单引号, 尝试使用双引号或在字符串中连接变量,如:

$ message ='嘿steve我的名字是:'。$ contact_name。'生病致敬!';