<?php
var_dump($_POST);
if(isset($_POST['email'])
&& isset($_POST['name'])) {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
}
else {
$name = $_POST["name"];
var_dump($name);
// check if name only contains letters and whitespace for first name
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Only letters and white space
allowed";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}
else {
$email = $_POST["email"];
var_dump($email);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if($data = $_POST['name'] . "\n" .
$_POST['email'] . "\n" .
$_POST['phone'] . "\n" .
$_POST['county'] . "\n" .
$_POST['floor'] . "\n" .
$_POST['descr'] . "\n"){
echo "Thank you for your inquery!" . "<br/>" .
"an estimator will be with you shortly.";
mail('myemail@gmail.com', 'Estimation Request',
$data);
}
}
else {
die('no post data to process');
}
?>
输出:
array(5){[&#34; name&#34;] =&gt; string(12)&#34; Nicholas Cox&#34; [&#34;电子邮件&#34;] =&GT;串(6) &#34; ######&#34; [&#34;电话&#34;] =&GT; string(10)&#34; 0000000000&#34; [&#34;县&#34;] =&GT;串(8) &#34;皮内拉斯&#34; [&#34; DESCR&#34;] =&GT; string(15)&#34; Thesh&#34; }
string(12)&#34; Nicholas Cox&#34; string(6)&#34; ######&#34;
谢谢你的询问! 很快就会有一个估算工具。\ n \ n无效的电子邮件格式\ n
注意:如果我错过了我的一些故障排除步骤,如随机回声或模具功能,请告诉我,以便我可以编辑到 帮助您更轻松地阅读
我没有在其他字段上添加验证。在添加其他人之前,我只是想让电子邮件和名称工作。
现在,当我添加随机内容时,它仍然会提示我的错误消息,但它仍然允许将任何内容放入文本字段并发送电子邮件。我在youtube上观看了一些教程,但我仍然无法做到正确。
答案 0 :(得分:0)
使用代码验证可以执行的操作。
<?php
var_dump($_POST);
$errorArr = array(); //Error Messages in array
if(isset($_POST['email'])
&& isset($_POST['name'])) {
if (empty($_POST["name"])) {
$errorArr['nameErr'] = "Name is required"; //Error Messages in array
}
else {
$name = $_POST["name"];
var_dump($name); // This should be commented
// check if name only contains letters and whitespace for first name
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$errorArr['nameErr'] = "Only letters and white space
allowed";//Error Messages in array
}
}
if (empty($_POST["email"])) {
$errorArr['emailErr'] = "Email is required";
}
else {
$email = $_POST["email"];
var_dump($email); // This should be commented
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errorArr['emailErr'] = "Invalid email format";
}
}
//Check Condition for any error
if(empty($errorArr)){
$data = $_POST['name'] . "\n" .
$_POST['email'] . "\n" .
$_POST['phone'] . "\n" .
$_POST['county'] . "\n" .
$_POST['floor'] . "\n" .
$_POST['descr'] . "\n"
echo "Thank you for your inquery!" . "<br/>" .
"an estimator will be with you shortly.";
mail('theshadowcallsu@gmail.com', 'Estimation Request',
$data);
} else {
var_dump($errorArr); //Show Error Message
}
}
else {
die('no post data to process');
}
?>
答案 1 :(得分:0)
虽然还有其他问题,但是你在这里问的问题就在于这个问题:
if ($data = $_POST['name'] . "\n" .
$_POST['email'] . "\n" .
$_POST['phone'] . "\n" .
$_POST['county'] . "\n" .
$_POST['floor'] . "\n" .
$_POST['descr'] . "\n")
我真的不知道你背后的意图是什么,但让我们把它拆开:
$_POST
包含字符串.
连接字符串\n
是换行符因此,整个事情,简化,归结为:
if ($data = $someLongStringFromMyPostValues)
现在,$data = $something
是assignment和expression。在PHP(和许多其他语言)中,这样的赋值评估(返回)已分配的值。换句话说,这会将您的$data
变量设置为长字符串,但关于条件,这几乎相当于:
if ($someLongStringFromMyPostValues)
由于PHP中的type juggling,a string with something (other than 0
) in it will be true
。看到你如何在字符串中添加换行符,这意味着,无论您的$_POST
值是什么,这总是会评估为(再次,暂时忽略$data
):
if (true)
您绝不会考虑之前设置的$nameErr
或$emailErr
变量。因此,无论您的意图是什么,您都需要修复此条件并将错误变量合并到其中。