开发者

Simplest way to stop email submission if field is empty

开发者 https://www.devze.com 2023-03-08 07:07 出处:网络
Im trying to duplicate my client side validation on the server (PHP). What is the simplest way to make sure a field has been filled out? I\'ve done it like this before but am having some issues, is th

Im trying to duplicate my client side validation on the server (PHP). What is the simplest way to make sure a field has been filled out? I've done it like this before but am having some issues, is there a better way?

<?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>
                 开发者_StackOverflow中文版   </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>

I need "LeadBlind" to be empty, it is a hidden input that only form filling bots would fill in, so if it has a value it should trigger the error. The ajax function is based on what is returned, if it's "error" it displays an error msg, otherwise a success one. thx for the help!

0

精彩评论

暂无评论...
验证码 换一张
取 消