PHP联系表 - 验证和

时间:2014-01-12 21:25:23

标签: php forms validation

我是php的新手,一直试图弄清楚如何正确验证电子邮件地址,并且数据已经输入到文本框中。我无法真正找到我需要的东西,并尝试按照php.net上的示例将我发送到一个圆圈。任何帮助将不胜感激!另外,我甚至用这种形式朝着正确的方向前进?该表单有效,我收到一封电子邮件,格式化为我希望在下拉框中的任一电子邮件地址。

-UPDATE - 我重写了一些我的脚本......有人可以检查一下,我现在遇到了更多问题。即使表格中没有输入任何内容,它也会发送一封电子邮件,即使您这样做也会发送任何内容。示例“email”test @ example正在通过。

<?php

//Sainitize function
function sanitizeString($value){
$value = strip_tags($value);
$value = trim($value);
$value = escapeshellcmd($value);
$value = htmlentities($value);

return $value;
}

$send = $_POST[send];

//Email validation - does not work by the way
if (filter_var($from, FILTER_VALIDATE_EMAIL)) {
$email_error = true;
$error_message[] = "Please use a valid email format: name@domain.com";
}     

if($send == 1){$email_sent = true; $step_1 = "complete";}
else{$email_sent = false; $step_1 = "complete";}

if($email_sent === true) {

$from = sanitizeString($_POST['from']);
$to = sanitizeString($_POST['to']);
$name = sanitizeString($_POST['name']);
$title = sanitizeString($_POST['title']);
$company = sanitizeString($_POST['company']);
$phone = sanitizeString($_POST['phone']);
$subject = sanitizeString($_POST['subject']);
$message = sanitizeString($_POST['message']);

// define variables and initialize with empty values
$nameErr = $addressErr = $emailErr = $messageErr = $phoneErr = "";
$name = $address = $email = $message = $phone = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {

    $nameErr = "Please enter your name.";
}
else {
    $name = $_POST["name"];
}

if (empty($_POST["email"])) {
    $emailErr = "Please enter your email."; 
}
else {
    $email = $_POST["email"];
}
if (empty($_POST["phone"])) {
    $phoneErr = "Please enter a phone number.";
}
else {
    $phone = $_POST["phone"];
}
if (empty($_POST["message"]))  {
    $messageErr = "Cannot leave message box blank."; 
}
else {
    $message = $_POST["message"];
}

}

//select the correct to address
switch ($to) {
case "1":
$to = "contact1@example.com";
break;
case "2":
$to = "contact2@example.com";
break;
default:
$to = "contact1@example.com";
break;}

if($message_error !== true && $email_error !== true){
$email_headers = "From:".$from."\nMIME-Version: 1.0 \nContent-type: text/html; charset=iso-8859-1";

$message_send = "<h3>".$name."<br>".$title."<br>".$company."<br>".$phone."<br>".$from."</h3><hr><h4>".$subject."</h4>".$message;

if (mail($to, $subject, $message_send, $email_headers)) {$error_message = "Thank you, your email is on the way!";}
else {$error_message = "There seems to be a problem!";}}

}

?>
<body>

<form action="<?php ($_SERVER["PHP_SELF"]);?>" method="post">
<table style="border-collapse:collapse; border-spacing:0" >
<tr>
<td>Name:</td>
<td><input name="name" placeholder="Name*" type="text" class="text"/>
        <span class="error"><?php echo $nameErr;?></span></td>
</tr>
<tr>
<td>Title:</td>
<td><input type="text" placeholder="Title" name="title" size="50"/></td>
</tr>
<tr>
<td>Company:</td>
<td><input type="text" placeholder="Company" name="company" size="50" /></td>
</tr>
<tr>
<td>Phone:</td>
<td>
    <input name="phone" placeholder="Phone*" type="tel" size="10" maxlength="10" value="<?php echo htmlspecialchars($phone);?>"/>
    <span class="style1">Example: 1234567890</span> <span class="error" style="color:#990000"><?php echo $phoneErr;?></span></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="from" placeholder="Email*" type="email" class="text" value="<?php echo htmlspecialchars($email);?>">
    <span class="error"><?php echo $emailErr;?></span></td>
</tr>
<tr>
<td>To:</td>
<td><select name="to" size="1">
    <option value="1">Contact 1</option>
    <option value="2">Contact 2</option>
    </select></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type="text" name="subject" placeholder="Subject" size="50" /></td>
</tr>
<tr>
<td valign="top">Detail:</td>
<td colspan="2"><textarea cols="50" rows="4" name="message" placeholder="Type your message here."></textarea></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;"><input type="hidden" name="send" value="1" /><input type="submit" value="Send" name="email_1" /></td>
</tr>

</table >
</form>

3 个答案:

答案 0 :(得分:0)

对于电子邮件验证,您可以使用filter_var函数和FILTER_VALIDATE_EMAIL过滤器属性。 Here是关于输入验证的好文章。从php手册中试试这个:

var_dump(filter_var('bob@example.com', FILTER_VALIDATE_EMAIL));

你可以简化你的代码,它有点乱。 switch电子邮件地址不是很好的做法。您可以像这样添加值:

<select name="to" size="1">
    <option value="contact1@example.com">Contact1</option>
    <option value="contact2@example.com">Contact2</option>
</select>

您还使用了已弃用的函数mysql_escape_stringltrimrtrim可以替换为trim函数。

<强>更新

您的代码中仍然存在很多错误。你有错误报告吗?电子邮件切换绝对不是解决您问题的好方法。看看重构的代码,它应该适合你:

<?php

//Sainitize function
function sanitizeString($value)
{
    $value = strip_tags($value);
    $value = trim($value);
    $value = escapeshellcmd($value);
    $value = htmlentities($value);

    return $value;
}

$errorMessage = array();
$receivers = array(
    1 => 'contact1@example.com',
    2 => 'contact2@example.com'
);

if(isset($_POST['form']))
{
    $formData = $_POST['form'];

    if (filter_var($formData['from'], FILTER_VALIDATE_EMAIL)) {
        $from = sanitizeString($formData['from']);
    }
    else
    {
        $errorMessage[] = "Please use a valid email format: name@domain.com";
    }

    if(array_key_exists($formData['to'], $receivers))
    {
        $to = $receivers[$formData['to']];
    }
    else
    {
        $to = 'default@example.com';
    }

    if(strlen($formData['name']) > 0)
    {
        $name = sanitizeString($formData['name']);
    }
    else
    {
        $errorMessage[] = "Please enter your name.";
    }

    if(strlen($formData['title']) > 0)
    {
        $title = sanitizeString($formData['title']);
    }
    else
    {
        $title = '';
    }

    if(strlen($formData['company']) > 0)
    {
        $company = sanitizeString($formData['company']);
    }
    else
    {
        $company = '';
    }

    if(strlen($formData['phone']) > 0)
    {
        $phone = sanitizeString($formData['phone']);
    }
    else
    {
        $errorMessage[] = "Please enter a phone number.";
    }

    if(strlen($formData['subject']) > 0)
    {
        $subject = sanitizeString($formData['subject']);
    }
    else
    {
        $subject = '';
    }

    if(strlen($formData['message']) > 0)
    {
        $message = sanitizeString($formData['message']);
    }
    else
    {
        $errorMessage[] = 'Cannot leave message box blank.';
    }

    if (empty($errorMessage) && $formData['spam'] == 9)
    {
        $email_headers = "From:" . $from . "\nMIME-Version: 1.0 \nContent-type: text/html; charset=iso-8859-1";
        $message_send = "<h3>" . $name . "<br>" . $title . "<br>" . $company . "<br>" . $phone . "<br>" . $from . "</h3><hr><h4>" . $subject . "</h4>" . $message;

        if (mail($to, $subject, $message_send, $email_headers))
        {
            $errorMessage[] = 'Thank you, your email is on the way!';
        }
        else
        {
            $errorMessage[] = 'There seems to be a problem!';
        }
    }
}
?>
<body>

<?php if(!empty($errorMessage)): ?>
    <div style="border: 2px solid red">
        <ul>
            <?php foreach ($errorMessage as $error): ?>
                <li><?php echo $error; ?></li>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <table style="border-collapse:collapse; border-spacing:0">
        <tr>
            <td>Name:</td>
            <td>
                <input name="form[name]" placeholder="Name*" type="text" class="text" value="<?php echo isset($name) ? $name : ''; ?>"/>
            </td>
        </tr>
        <tr>
            <td>Title:</td>
            <td>
                <input type="text" placeholder="Title" name="form[title]" size="50" value="<?php echo isset($title) ? $title : ''; ?>"/>
            </td>
        </tr>
        <tr>
            <td>Company:</td>
            <td>
                <input type="text" placeholder="Company" name="form[company]" size="50" value="<?php echo isset($company) ? $company : ''; ?>"/>
            </td>
        </tr>
        <tr>
            <td>Phone:</td>
            <td>
                <input name="form[phone]" placeholder="Phone*" type="tel" size="10" maxlength="10" value="<?php echo isset($phone) ? $phone : ''; ?>"/>
                <span class="style1">Example: 1234567890</span>
            </td>
        </tr>
        <tr>
            <td>Email:</td>
            <td>
                <input name="form[from]" placeholder="Email*" type="email" class="text" value="<?php echo isset($from) ? $from : ''; ?>">
            </td>
        </tr>
        <tr>
            <td>To:</td>
            <td>
                <select name="form[to]" size="1">
                    <option value="1">Contact 1</option>
                    <option value="2">Contact 2</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Subject:</td>
            <td>
                <input type="text" name="form[subject]" placeholder="Subject" size="50" value="<?php echo isset($subject) ? $subject : ''; ?>"/>
            </td>
        </tr>
        <tr>
            <td valign="top">Detail:</td>
            <td colspan="2">
                <textarea cols="50" rows="4" name="form[message]" placeholder="Type your message here."><?php echo isset($message) ? $message : ''; ?></textarea>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                3x3 = <input type="text" value="" name="form[spam]"/>
                <input type="submit" value="Send" />
            </td>
        </tr>
    </table>
</form>

答案 1 :(得分:0)

我在php.net上发现了这个..它有用吗?

if (filter_var($from, FILTER_VALIDATE_EMAIL)) {
   $email_error = true;
   $error_message[] = "Please use a valid email format: name@domain.com";
} 

答案 2 :(得分:0)

Filter var确实有效。请尝试以下代码。

<?php
   $emailError = array();

   if(isset($_POST["send"])){

   $from = $_POST["from"];

   if (!filter_var($from, FILTER_VALIDATE_EMAIL)) {
   $emailError[] = "Please use a valid email format: name@domain.com\n\r";
   } 
   else {
    echo $from . " is a valid email.\n\r";
   }

   if ($emailError){
     foreach ($emailError as $key){
     echo $key;
     }
   }

} else {

?> 

  <form action="<?php ($_SERVER["PHP_SELF"]);?>" method="post">
  <table>
  <tr>
  <td>Email:</td>
  <td>
  <input name="from" placeholder="Email*" type="email" class="text" value="">
  </td>
  </tr>
  </table>
  <input type="submit" value="Send" name="send" />
  </form>

<?php
 }
?>