【问题标题】:My form validations work but seem to be submitting blank sometimes with a hack我的表单验证有效,但有时似乎提交空白
【发布时间】:2014-02-18 15:28:44
【问题描述】:

我的表单有 JavaScript 验证,当字段为空时会返回错误并停止表单提交。 ""

我还在后端检查任何空字段的 PHP 验证。

提交会发送一封包含字段的电子邮件。

在测试所有验证工作。正确填写后,表格将提交所有信息。

我大约每天都会收到一封空白电子邮件。提交如何绕过我的 JavaScript 和 PHP 验证?

我的表格:

<form id="itsp-form" method="post" action="http://www.website.com/save_itsp.php">
<label class="custom">Company name</label>
<input id="company_name" type="text" name="company_name" />

<label class="custom">Company URL</label>
<input id="company_url" type="text" name="company_url" />

<label class="custom">Company address</label>
<input id="company_address" type="text" name="company_address" />

<label class="custom">Type of business</label>
<select id="type_of_business[]" name="type_of_business[]" multiple="multiple">
  <option value="enterprise">Business sector/Enterprise</option>
  <option value="residential">Residential</option>
  <option value="wholesale">Wholesale VoIP Carrier</option>
  <option value="other">Other</option>
</select>

<label class="custom">Areas served</label>
<select id="areas_served[]" name="areas_served[]" multiple="multiple">
  <option value="USA">USA</option>
  <option value="Canada">Canada</option>
  <option value="other">Other</option>
</select>

<br />

<label class="custom">Sales contact</label><br />
<h4>Name</h4>
  <input id="sales_name" type="text" name="sales_name" />
<h4>Phone</h4>
  <input type="text" name="sales_phone" />
<h4>Email</h4>
  <input type="text" name="sales_email" />

<br />

<label class="custom">Testing contact</label><br />
<h4>Name</h4>
  <input id="testing_name" type="text" name="testing_name" />
<h4>Phone</h4>
  <input type="text" name="testing_phone" />
<h4>Email</h4>
  <input type="text" name="testing_email" />

<br />

<label class="custom">Switch Platform</label>
<select id="switch_platform[]" name="switch_platform[]" multiple="multiple">
  <option value="asterisk">Asterisk</option>
  <option value="broadsoft">Broadsoft</option>
  <option value="metaswitch">Metaswitch</option>
  <option value="sipx">SipX/eZuce</option>
  <option value="other">Other</option>
</select>

<label class="custom">Interested In Testing</label>
<select id="interested_in_testing[]" name="interested_in_testing[]" multiple="multiple">
  <option value="atas">ATAs</option>
  <option value="ip_phones">IP Phones</option>
  <option value="gateways">Gateways</option>
  <option value="ip_pbx">IP PBX</option>
</select>

<input type="submit" id="submit" value="Submit" />

</form>
<div id="errors"></div>
</div>
 <script>
  $('#submit').click(function() {
    $('.error').hide();

    var hasError = false;
    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

    if (($("#company_name").val() == '') || ($("#type_of_business[]").val() == '')) {
       $("#errors").after('<span class="error">Please enter your Company name.</span>');
       hasError = true;
    }
    if (($("#company_url").val() == '') || ($("#company_address").val() == '')) {
       $("#errors").after('<span class="error">Please enter your Company information.</span>');
       hasError = true;
    }
    if ($("#areas_served[]").length < 0) {
       $("#errors").after('<span class="error">Please enter your Areas served.</span>');
       hasError = true;
    }
    if ($("#type_of_business[]").length < 0) {
       $("#errors").after('<span class="error">Please enter your Type of business.</span>');
       hasError = true;
    }
    if ($("#sales_name").val() == '') {
       $("#errors").after('<span class="error">Please enter your Sales contact information.</span>');
       hasError = true;
    }
    if ($("#testing_name").val() == '') {
       $("#errors").after('<span class="error">Please enter your Tester contact information</span>');
       hasError = true;
    }
    if ($("#switch_platform[]").length < 0) {
       $("#errors").after('<span class="error">Please enter your Switch platform</span>');
       hasError = true;
    }
    if ($("#interested_in_testing[]").length < 0) {
       $("#errors").after('<span class="error">Please enter your Testing interests.</span>');
       hasError = true;
    }
  if(hasError == true) { return false; }
});

我的 PHP 文件:

function died($error) {
// your error code can go here
echo "We are very sorry, but there were blank fields found with the form you
   submitted. ";
$link_address = 'http://www.website.com/url/itsp';
echo "<a href='".$link_address."'>Click to Go Back<br/></a>";
die();
}


if (isset($_POST['company_name'])) 
{
$errors = "";

//validate and sanitize company name
if ($_POST['company_name'] != "")
{
  $_POST['company_name'] = filter_var($_POST['company_name'], FILTER_SANITIZE_STRING);
  $company_name = $_POST['company_name'];
}
else 
died();

//validate and sanitize company url
if ($_POST['company_url'] != "")
{
  $_POST['company_url'] = filter_var($_POST['company_url'], FILTER_SANITIZE_STRING);
  $company_url = $_POST['company_url'];
}
else
    died();
//validate and sanitize company address
if ($_POST['company_address'] != "")
{
  $_POST['company_address'] = filter_var($_POST['company_address'], FILTER_SANITIZE_STRING);
  $company_address = $_POST['company_address'];
}
else
    died();
if (is_array($_POST['type_of_business']) && !empty($_POST['type_of_business'])) {
    $type_of_business_val = array();
    foreach($_POST['type_of_business'] as $val) {
        $type_of_business_val[] = $val;
    }
    $type_of_business = implode(',', $type_of_business_val);
}
else
    died();
if (is_array($_POST['areas_served']) && !empty($_POST['areas_served'])) {
    $areas_served_val = array();
    foreach($_POST['areas_served'] as $val) {
        $areas_served_val[] = $val;
    }
    $areas_served = implode(',', $areas_served_val);
}
else
    died();
//validate and sanitize sales name
if ($_POST['sales_name'] != "")
{
  $_POST['sales_name'] = filter_var($_POST['sales_name'], FILTER_SANITIZE_STRING);
  $sales_name = $_POST['sales_name'];
}
else
    died();
//validate and sanitize sales email
if ($_POST['sales_email'] != "")
{
  $sales_email = filter_var($_POST['sales_email'], FILTER_SANITIZE_EMAIL);
  if (!filter_var($sales_email, FILTER_VALIDATE_EMAIL)) {
      $errors .= "$sales_email is <strong>NOT</strong> a valid email address.<br/><br/>";
  }
}
else
    died();
//validate and sanitize sales phone number
if ($_POST['sales_phone'] != "")
{
  $_POST['sales_phone'] = filter_var($_POST['sales_phone'], FILTER_SANITIZE_STRING);
  $sales_phone = $_POST['sales_phone'];
}
else
    died();
//validate and sanitize testing name
if ($_POST['testing_name'] != "")
{
  $_POST['testing_name'] = filter_var($_POST['testing_name'], FILTER_SANITIZE_STRING);
  $testing_name = $_POST['testing_name'];
}
else
    died();
//validate and sanitize testing email
if ($_POST['testing_email'] != "")
{
  $testing_email = filter_var($_POST['testing_email'], FILTER_SANITIZE_EMAIL);
  if (!filter_var($testing_email, FILTER_VALIDATE_EMAIL)) {
      $errors .= "$testing_email is <strong>NOT</strong> a valid email address.<br/><br/>";
  }
}
else
    died();
if ($_POST['testing_phone'] != "")
{
  $_POST['testing_phone'] = filter_var($_POST['testing_phone'], FILTER_SANITIZE_STRING);
  $testing_phone = $_POST['testing_phone'];
}
else
    died();
if (is_array($_POST['switch_platform']) && !empty($_POST['switch_platform'])) 
{
    $switch_platform_val = array();
    foreach($_POST['switch_platform'] as $val) {
        $switch_platform_val[] = $val;
    }
    $switch_platform = implode(',', $switch_platform_val);
}
else
    died();

if (is_array($_POST['interested_in_testing']) && !empty($_POST['interested_in_testing'])) {
    $interested_in_testing_val = array();
    foreach($_POST['interested_in_testing'] as $val) {
        $interested_in_testing_val[] = $val;
    }
    $interested_in_testing = implode(',', $interested_in_testing_val);
}
else
    died();
 }
 /************** End Validations *******************/

 /*****Email*****/
$to = "email";
$subject = "New ITSP Submission";
$message1 = "A new ITSP has submitted their information:
<br/>Company Name: " . $company_name . "
<br/>Company URL: " . $company_url . "
<br/>Company Address: " . $company_address . "
<br/>Type of Business: " . $type_of_business . "
<br/>Area(s) Served: " . $areas_served . "
<br/>Sales Name: " . $sales_name . "
<br/>Sales Email: " . $sales_email . "
<br/>Sales Phone: " . $sales_phone . "
<br/>Testing Name: " . $testing_name . "
<br/>Testing Email: " . $testing_email . "
<br/>Testing Phone: " . $testing_phone . "
<br/>Switch Platform: " . $switch_platform . "
<br/>Interested In Testing: " . $interested_in_testing ;


$headers = "MIME-Version: 1.0\n";

mail($to,$subject,$message1,$headers);
header("location: http://www.website.com/dir/itsp-confirmation/");

【问题讨论】:

  • 字符串" "(空格,所以为空但长度> 1)会通过,试试trim()

标签: javascript php jquery forms validation


【解决方案1】:

如果电子邮件验证器失败,您不会调用您的函数 dead(),但前提是电子邮件为空。

您的 FILTER_SANITIZE_EMAIL 可以返回一个空字符串。 例如,如果您的 $_POST['sales_email'] 等于空格或电子邮件过滤器删除的任何字符,您将得到一个空字符串。

试试这个:

if ($_POST['sales_email'] != "")
{
  $sales_email = filter_var($_POST['sales_email'], FILTER_SANITIZE_EMAIL);
  if (!filter_var($sales_email, FILTER_VALIDATE_EMAIL)) {
      $errors .= "$sales_email is <strong>NOT</strong> a valid email address.<br/><br/>";
      died();
  }
}
else
    died();

【讨论】:

  • 对于选择框,他们怎么会不验证? 'switch_platform' 正在检查它是否为空。然而,黑客电子邮件在该字段中却一无所获。
【解决方案2】:

您可以在表单的输入字段中添加必填项,以确保字段在提交之前已输入文本。如果字段未通过验证,filter_var() 也可能返回 false,结果可能在您的电子邮件中显示为空白。

【讨论】:

  • 如果 filter_var() 返回 false,用户将被引导到一个页面,告诉他们错误消息和返回链接以重试。电子邮件不会发送。我测试过了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-16
  • 2020-09-22
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多