如果字段为空,则最简单的方法是停止提交电子邮件

时间:2011-05-23 19:44:32

标签: php validation

我试图在服务器(PHP)上复制我的客户端验证。确保字段填写的最简单方法是什么?我以前做过这样的事情,但我遇到了一些问题,还有更好的办法吗?

<?php
if(empty($_POST['Contact0FirstName']) || empty($_POST['Contact0Email']) ||
empty($_POST['Contact0Phone1']) || empty($_POST['CaptchaInput']) || !empty($_POST['LeadBlind'])) {
echo "Error";
die();
} else {
//set POST variables
$url = 'xxx';
$infusion_xid = $_POST["xxx"];
$infusion_type = $_POST["xxx"];
$infusion_name = $_POST["xxx"];
$Contact0FirstName = $_POST["Contact0FirstName"];
$Contact0Email = $_POST["Contact0Email"];
$Contact0Phone1 = $_POST["Contact0Phone1"];


$fields = array(
    'infusion_xid'=>urlencode($infusion_xid),
    'infusion_type'=>urlencode($infusion_type),
    'infusion_name'=>urlencode($infusion_name),
    'Contact0FirstName'=>urlencode($Contact0FirstName),
    'Contact0Email'=>urlencode($Contact0Email),
    'Contact0Phone1'=>urlencode($Contact0Phone1)

);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'& ');

//open connection
$ch = curl_init($url);

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//their return html
$output = curl_exec($ch);
//echo "TEST";
echo $output;
curl_close($ch);
}
?>

<form id="Lead" method="post" action="PHP/Lead.php" accept-charset="UTF-8">

                <ul id="LeadForm">
                    <li>*required field</li>
                    <li>
                        <input type="hidden" name="LeadBlind" id="LeadBlind">
                        <label for="Contact0FirstName">1 | First Name*</label>
                        <label for="Contact0Email">2 | Email*</label>
                        <label for="Contact0Phone1">3 | Daytime Phone*</label>
                    </li>
                    <li>
                        <input type="text" name="Contact0FirstName" id="Contact0FirstName">
                        <input type="text" name="Contact0Email" id="Contact0Email">
                        <input type="text" name="Contact0Phone1" id="Contact0Phone1">
                    </li>

                    <li>
                        <label for="CaptchaInput">4 | Please enter the code</label>
                    </li>
                    <li>
                        <input type="text" class="numbers" name="Captcha" id="Captcha" value="" readonly>
                        <input type="text" class="numbers" name="CaptchaInput"  id="CaptchaInput" size="6" maxlength="6">
                    </li>
                    <li>
                        <input type="submit" id="LeadSend" value="Try It Now!">
                        <span id="processing">Submitting Your Request</span>
                    </li>                  
                </ul>
                <div class="clear"></div>
            </form>

我需要“LeadBlind”为空,它是一个隐藏的输入,只能形成填充机器人填充,所以如果它有一个值,它应该触发错误。 ajax函数基于返回的内容,如果它是“错误”则显示错误消息,否则为成功消息。请求帮助!

0 个答案:

没有答案