如何在我的目标网页自定义邮件程序中将optin表单字段设为必填项

时间:2015-02-24 09:16:21

标签: php email

我在登录页面上使用以下代码作为我的optin表单邮件程序

<?php
/* Set e-mail recipient */

$myemail  = "MYEMAIL@EMAIL.COM";

/* Check all form inputs using check_input function */
$fname    = check_input($_POST['fname'], "Enter your first name");
$lname    = check_input($_POST['lname'], "Enter your last name");
$email    = check_input($_POST['email']);
$phone    = check_input($_POST['phone']);
$resort   = check_input($_POST['resort']);
$amount   = check_input($_POST['amount']);
$call     = check_input($_POST['call']);

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:


First Name : $fname
Last Name : $lname
E-mail: $email
Phone : $phone
Resort: $resort
Amount: $amount
Call  : $call

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);

/* Redirect visitor to the thank you page */
header('Location: index.html');
exit();

/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
if (strtolower($_POST['code']) != 'mycode') {die('Wrong access code');}
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>

现在,当人们提交表单时,我想要填写所有必填字段,并在他/她错过字段时显示错误消息。

0 个答案:

没有答案