我正在尝试跟踪提交时网站表单上留空的字段。
我正在使用的表格是由我的销售代理使用的,由于在销售代理有机会修改提交的信息之前我没有立即看到提交的信息,所以我想看看他们是否填写了所有网站上的信息,而不是稍后在 CRM 中添加更多信息。这将帮助我优化表格以供其使用。目前该表单是使用 HTML、PHP、jQuery 和 AJAX 编写的。
我还没有尝试过任何东西,因为我什至不知道从哪里开始。我以前没见过这样做。
如果您需要查看标记或者此问题是否需要更多说明,请告诉我。如果最简单的话,请随时为我指出教程的方向。
感谢您的帮助!
最佳答案
这就是 PHP empty() 的用途:
if (empty(trim($_POST['some_field']))
{
// Nothing was supplied.
}
因此,您可以创建一个“必填”字段数组,如下所示:
$required = array('this', 'that', 'the', 'other');
...然后循环它们:
$errors = false;
foreach ($required as $field)
{
$field_value = isset($_POST[$field]) ? trim($_POST[$field]) : null;
if (empty($field_value))
{
$errors[] = 'The "' . $field . '" field was not submitted.';
}
}
if ($errors)
{
// Do something now that you know there are missing fields.
// Here, we're sending an email...
$subject = 'These fields were not filled out';
$body = 'The following errors occurred: ' . implode(', ', $errors);
mail('email@example.com', $subject, $body);
}
关于javascript - 空白场跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25394389/