0

我的联系表没有通过电子邮件发送电话号码。我在表单和脚本中都有该字段。

<form id="contact" action="">
    <fieldset>
        <label for="name" id="name_label">Name <br /></label>
        <input type="text" name="name" id="name"  value="" class="text-input" />
        <label for="email" id="email_label">E-mail<br /></label>
        <input type="text" name="email" id="email"  value="" class="text-input" />
        <label for="phone" id="phone_label">Phone number<br /></label>
        <input type="text" name="phone" id="phone"  value="" class="text-input" />
        <label for="msg" id="msg_label">Preferred contact time</label>
        <textarea cols="60" rows="10" name="msg" id="msg" ></textarea> <br class="clear" />
        <input type="submit" name="submit" class="button" id="submit_btn" value="Send Message &rarr;"/>
    </fieldset>
    </form>

和以下 PHP 脚本:

<?php 
    $emailTo = 'test@test.co.uk';
    $subject = 'Advanced Contact Form';
    $name=$_REQUEST['name'];
    $email=$_REQUEST['email'];
    $phone=$_REQUEST['phone'];
    $msg=$_REQUEST['msg'];

    $body = "Name: $name \n\nEmail: $email \n\nphone: $phone \n\nMessage: $msg";
    $headers = 'From: '.$name.' <'.$email.'>' . "\r\n" . 'Reply-To: ' . $email;

    mail($emailTo, $subject, $body, $headers);
    echo 'Mail sent';
?>

电子邮件发送正常,但电话号码未显示在电子邮件中。任何想法可能是什么问题?

编辑:我的网址如下所示:

www.website.com/contact.html?name=test&email=test%40test.com&phone=0112+2255555&msg=2312312312312&submit=Send+Message+%26%238594%3B
4

1 回答 1

1

这是一个基本示例,说明您应该如何处理电子邮件表单,首先处理值并在错误数组(如果有)的基础上构建。

然后发送,永远不要相信用户输入,尤其是联系表格。

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){

$emailTo = 'test@test.co.uk';
$subject = 'Advanced Contact Form';

$name=(isset($_POST['name'])?$_POST['name']:null);
$email=(isset($_POST['email'])?$_POST['email']:null);
$phone=(isset($_POST['phone'])?$_POST['phone']:null);
$msg=(isset($_POST['msg'])?$_POST['msg']:null);

$error=array();
$cont=true;
//name
if(isset($name)){
    if(strlen($name)<=1){
        $cont=false;
        $error['error_name']='Enter your real name!';
    }else{
        $name=preg_replace('/[^a-zA-Z0-9\(\)\:\?.\&,_ -]/s', '', $name);
    }
}else{
    $error['error_name']='Enter your name!';
    $cont=false;
}

//email
if(isset($email)){
    if(filter_var($email, FILTER_VALIDATE_EMAIL)){

    }else{
        $cont=false;
        $error['error_email']='Email Invalid';
    }
}else{
    $cont=false;
    $error['error_email']='Please enter your email!';
}

//phone
if(isset($phone)){
    if(strlen($phone)<=1){
        $cont=false;
        $error['error_phone']='Enter your real number!';
    }else{
        $phone=preg_replace('/[^0-9]/s', '', $phone);
    }
}else{
    $error['error_phone']='Enter your number!';
    $cont=false;
}

//message
if(isset($msg)){
    if(strlen($msg)<=1){
        $cont=false;
        $error['error_message']='Enter your real message!';
    }else{
        $msg=preg_replace('/[^a-zA-Z0-9\(\)\:\?.\&,_ -]/s', '', $msg);
    }
}else{
    $error['error_message']='Enter your message!';
    $cont=false;
}

if(empty($error) && $cont ==true){
    //send mail
    $body = "Name: $name \n\n
         Email: $email \n\n
         phone: $phone \n\n
         Message: $msg";

    // Additional headers
    $headers ='MIME-Version: 1.0'."\r\n";
    $headers.='Content-type: text/html; charset=iso-8859-1'."\r\n";
    $headers.='Reply-To: '.$email."\r\n";
    $headers.='From: '.$name.' <'.$email.'>'."\r\n";
    $headers.="X-Mailer: Remote Mail\r\n";
    // Mail it
    $status = (mail($emailTo, $subject, $body, $headers))?'Mail Was Sent':'Error Sending email';
}else{

  //error with form values
  $status='';
  foreach($error as $type=>$reason){
    $status .= $reason.'<br />';
  }
}

//Echo your form or status page
echo $status;
}else{
    //Echo your form
}

?>
于 2012-05-08T16:28:43.643 回答