【问题标题】:PHP Form only emails 1 valuePHP 表单仅发送 1 个值
【发布时间】:2014-10-19 08:03:16
【问题描述】:

当我提交我的 html 表单时,我只会在我的电子邮件中获得 $name 值。此外,当我单击提交而不是重定向到我的感谢 html 页面时,感谢页面的内容会出现在表单下方。任何想法为什么?这是我的代码...

HTML:

<section class="container content">

<div class="row">
<div class="center title col-sm-12">
<h2>Short Application Form</h2>
</div>
</div> 

<h2>Personal Information</h2>
<form method="post" action="appemail.php" name="contactform" id="contactform" class="row">
<fieldset>
<div class="form-field col-sm-6">
<label for="name">First Name</label>
<span><input type="text" name="name" id="name"/></span>
</div>

<div class="form-field col-sm-6">
<label for="lastname">Last Name</label>
<span><input type="text" name="lastname" id="lastname"/></span>
</div>

<div class="form-field col-sm-6">
<label for="email">Email</label>
<span><input type="text" name="email" id="email"/></span>
</div>

<!--<div class="form-field col-sm-6">
<label for="email">Referral Name</label>
<span><input type="email" name="email" id="email"/></span>
</div>-->

<div class="form-field col-sm-6">
<label for="email">Home/Cell Phone</label>
<span><input type="text" name="phone" id="phone"/></span>
</div>

<div class="form-field col-sm-6">
<label>Martial Satus</label>
<select name="martial">
    <option>Married</option>
    <option>Unmaried</option>
    <option>Seperated</option>
</select>
</div>

<div class="form-field col-sm-6">
<label>Are you a Canadian Permanent Resident</label>
<select name="resident">
    <option>Yes</option>
    <option>No</option>
</select>
</div>

<div class="form-field col-sm-6">
<label for="email">Date of Birth</label>
<span><input type="text" name="birth"/></span>
</div>

<div class="form-field col-sm-6">
<label for="email">Number of Dependents</label>
<span><input type="text" name="dependents"/></span>
</div>

<div class="center title col-sm-12">
 <h2 style="text-align:left">Current Address</h2>
</div>

<div class="form-field col-sm-6">
<label for="name">Street Address</label>
<span><input type="text" name="adress"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">City</label>
<span><input type="text" name="city"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Province</label>
<span><input type="text" name="province"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Postal Code</label>
<span><input type="text" name="postal"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Years at this Address</label>
<span><input type="text" name="years"/></span>
</div>

<div class="form-field col-sm-6">
<label>Rent or Own</label>
<select name="rent">
    <option>Own</option>
    <option>Rent</option>
</select>
</div>

<div class="center title col-sm-12">
 <h2 style="text-align:left">Subject Property</h2>
 </div>

<div class="form-field col-sm-6">
<label>Property Usage</label>
<select name="property">
    <option>Primary Residence</option>
    <option>Second Home</option>
     <option>Investment</option>
</select>
</div>

<div class="form-field col-sm-6">
<label for="name">Loan Amount $ </label>
<span><input type="text" name="loan"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Purchase Price $</label>
<span><input type="text" name="price"/></span>
</div>

<div class="center title col-sm-12">
 <h2 style="text-align:left">Monthly Income</h2>
 </div>

 <div class="form-field col-sm-6">
<label for="name">Yearly Income $ </label>
<span><input type="text" name="yearly"/></span>
</div>
<div class="form-field col-sm-6">
<label for="name">Other Sources of Income (combined) $</label>
<span><input type="text" name="other"/></span>
</div>

<div class="form-field col-sm-12">
<label for="message">Comments</label>
<span><textarea name="message" id="message"></textarea></span>
</div>
</fieldset>
<div class="form-click center col-sm-12">
<span><input type="submit" name="submit" id="submit" value="Send it"/></span>
</div>
<div id="alert" class="col-sm-12"></div>
</form>
</section>

PHP:

<?php

$name = $_POST['name'];
$lastname = $_POST['lastname'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$martial = $_POST['martial'];
$resident = $_POST['resident'];
$birth = $_POST['birth'];
$dependents = $_POST['dependents'];
$adress = $_POST['adress'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal = $_POST['postal'];
$years = $_POST['years'];
$rent = $_POST['rent'];
$property = $_POST['property'];
$loan = $_POST['loan'];
$price = $_POST['price'];
$other = $_POST['other'];
$message = $_POST['message'];

if(IsInjected($visitor_email))
{
    echo "Bad email value!";
    exit;
}

$email_from = $visitor_email;
$email_subject = "Application Loan";

$email_body = "You have received a new message from $name\n\n".
    "Here is the Application: \n\n".
    "Phone: $phone\n\n".
    "Martial Status: $martial\n\n".
    "Resident: $resident\n\n".
    "Birth: $birth\n\n".
    "Dependents: $dependents\n\n".
    "Address: $address\n\n".
    "City: $city\n\n".
    "Province: $province\n\n".
    "Postal: $postal\n\n".
    "Years at this Address: $years\n\n".
    "Rent or Own: $rent\n\n".
    "Property: $property\n\n".
    "Loan: $loan\n\n".
    "Price: $price\n\n".
    "Other: $other\n\n".
    "Message: $message\n\n";

$to = "email@gmail.com";//<== update the email address
$headers = "From: $visitor_email \r\n";
$headers .= "Reply-To: $visitor_email \r\n";

//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location:thank-you.html');
// Function to validate against any email injection attempts

function IsInjected($str)
{
  $injections = array('(\n+)','(\r+)','(\t+)','(%0A+)','(%0D+)','(%08+)','(%09+)' );
  $inject = join('|', $injections);
  $inject = "/$inject/i";

  if(preg_match($inject,$str)){return true;}
  else{return false;}
}
?>

提前致谢。

【问题讨论】:

  • 军事 -> 婚姻 :)
  • 你真的应该注意字符编码/转义。否则,您的电子邮件可能会被转发服务器更改和修改。

标签: php html forms web html-email


【解决方案1】:

由于换行符,字符串连接不起作用。

【讨论】:

    【解决方案2】:

    重写你的代码并使用&lt;br&gt; 除了\n & 当使用 isset 函数提交表单检查数据时,添加一个隐藏字段。

    【讨论】:

    • 使用
      的语法会怎样?谢谢将添加isset功能。 @帕特里夏
    【解决方案3】:

    我已经复制了你的代码,它运行良好,除了你的 $address 有错别字。 有时您使用地址(这是正确的),有时您使用地址。

    我收到的电子邮件:

    You have received a new message from First Name
    Here is the Application:
    Phone: 0000000
    Martial Status: Married
    Resident: Yes
    Birth: 2014-10-19
    Dependents: 42
    Address: Street Address
    City: City
    Province: Province
    Postal: Postal Code
    Years at this Address: 42
    Rent or Own: Own
    Property: Primary Residence
    Loan: 42
    Price: 42
    Other: 42
    Message: Comments
    

    新的行显示将适用于大多数电子邮件客户端,因为没有 html 标签,因此它将显示为纯文本。 Setting the appropriate header 也有帮助。

    否则,你的后端是什么? 操作系统、主机、PHP 版本?

    【讨论】:

    • 我已经尝试更改标题,但我仍然只获得名称值。我还注意到,即使我将其更改为仅发送 1 个变量,唯一有效的变量是 $name... 我会尝试获取主机信息。 “您收到了来自 myname 的新消息 这是申请:电话:婚姻状况:居民:出生:家属:地址:城市:省:邮政:该地址的年限:出租或拥有:财产:贷款:价格:其他:消息:“
    • 在接收 php 脚本中设置 header 不会影响它作为输入获得的 POST 数据。无论如何,这不太可能是问题。由于您提供的代码或多或少有效(军事,地址),因此您的环境有些可疑。你使用任何框架(蛋糕、symfony、燃料等)吗? phpinfo(); 显示哪些版本?
    猜你喜欢
    • 2016-04-25
    • 1970-01-01
    • 2011-04-14
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 2014-02-27
    相关资源
    最近更新 更多