我正在尝试将表单添加到我的wordpress页面,但我现在遇到的问题是,只要页面在我的网页上访问,表单就会向我发送电子邮件。我认为这是因为我已经将php放在头文件中,所以每次加载头都会发送一封电子邮件。如何让它只在实际填写时发送表单提交电子邮件?
HTML
<form name="infoform" method="post" action="" >
<br><input type="text" name="ffirstname" placeholder="First Name"/><input type="text" name="lastname" placeholder="Last Name"/>
<br><br><input type="text" name="address" placeholder="Address"/><input type="text" name="unit" placeholder="Unit"/></br>
<br><input type="text" name="city" placeholder="City"/><select name="province" >
<option value="ab">AB</option>
<option value="BC">BC</option>
<option value="BC">MB</option>
<option value="NB">NB</option>
<option value="NL">NL</option>
<option value="NS">NS</option>
<option value="ON">ON</option>
<option value="PE">PE</option>
<option value="QC">QC</option>
<option value="SK">SK</option>
</select><input type="text" name="postal" placeholder="Postal Code"/></br>
<br><input type="text" name="country" placeholder="Country"/></br>
<Br><input type="text" name="email" placeholder="Email"/><input type="number" name="phone" placeholder="Phone#"/></br>
<br> <br><input class="submit" type="submit" value="Next" />
CSS
<style>
input[type=text], input[type=number], select[name=province]{ font-family: arial; width:100%;
border: 1px solid #E5E5E5; padding: 10px 20px;}
input[name=ffirstname] {width:49%; margin-right:1%; }
input[name=lastname] {width:49%; margin-left:1%; }
input[name=address] {width:65.6667%; margin-right:1%; }
input[name=unit] {width:32.3337%; margin-left:1%; }
input[name=city] {width:49%; margin-right:1%; }
select[name=province] {width:24%; margin-left:1%;}
input[name=postal] {width:24%; margin-left:1%; }
input[name=email] {width:49%; margin-right:1%; }
input[name=phone] {width:49%; margin-left:1%;}
input[class=submit] {
background-color: #f05a28;
color: white;
padding: 8px 20px;
margin-left: 85%;
border-radius: 4px;
border: none; !important;
outline: none; !important ;
cursor: pointer;
box-shadow: none; !important;}
PHP
<?php
$name = $_POST['ffirstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$unit = $_POST['unit'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$formcontent=" From: $name $lastname \n Address: $address
\n Unit: $unit \n City: $city \n Province: $province \n
Postal Code: $postal \n Country: $country \n
Email: $email \n Phone: $phone \n ";
$recipient = "contact@justhired.ca";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
?>
答案 0 :(得分:2)
您需要检查提交是否已设置且输入是否为空。
旁注:您可以添加其余的POST数组,并为您的<input class="submit" type="submit" value="Next" />
命名:
<input class="submit" name="submit" type="submit" value="Next" />
示例:
if(isset($_POST['submit'])) {
if
(
!empty($_POST['ffirstname'])
&&
!empty($_POST['lastname'])
// ... and any other inputs you wish to check for.
)
{
$name = $_POST['ffirstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$unit = $_POST['unit'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$formcontent=" From: $name $lastname \n Address: $address
\n Unit: $unit \n City: $city \n Province: $province \n
Postal Code: $postal \n Country: $country \n
Email: $email \n Phone: $phone \n ";
$recipient = "contact@justhired.ca";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}
}
<form name="infoform" method="post" action="" >
是您在电子邮件中获取空值的主要原因,因为您在与PHP处理器相同的文件中使用整个代码而不检查是否有任何输入是否空。
答案 1 :(得分:0)
<?php
if(isset($_POST['submit'])){
$name = $_POST['ffirstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$unit = $_POST['unit'];
$city = $_POST['city'];
$province = $_POST['province'];
$postal = $_POST['postal'];
$country = $_POST['country'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$formcontent=" From: $name $lastname \n Address: $address
\n Unit: $unit \n City: $city \n Province: $province \n
Postal Code: $postal \n Country: $country \n
Email: $email \n Phone: $phone \n ";
$recipient = "contact@justhired.ca";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
}
?>
根据Fred的建议,您需要检查表单是否已提交 并将提交按钮更改为
<input class="submit" type="submit" name="submit" value="Next" />
答案 2 :(得分:0)
正在发生的事情是每次页面加载时您的PHP脚本都在运行。您需要在PHP中包含一个条件语句:“单击此按钮时,请执行此操作...”
为了实现这一目标,您需要更新HTML并将name属性添加到提交按钮。我给该属性赋值为'Next'。你总是可以改变它。但是,请确保更改反映在PHP脚本中。
<br> <br><input name="Next" class="submit" type="submit" value="Next" />
然后在PHP脚本中,以下内容将包装整个脚本:
if(isset($_POST['Next'])){
//Your script goes here
}