【问题标题】:Get field names on unchecked checkboxes在未选中的复选框上获取字段名称
【发布时间】:2022-01-26 17:49:15
【问题描述】:

我为我的雇主创建了一个 HTML 申请表,以便申请人可以在线申请。但是,我遇到了 PHP 位的一些问题。我希望它向我发送一封电子邮件,其中包含所有表单字段名称及其值(即使该值留空)。它需要采用这种特定格式,以便我可以通过编程方式快速将电子邮件中的数据“转换”为 HCL Notes 表单。

但是,当我的 HTML 表单上的复选框未选中时,它根本不会发送到 $_POST 数组,这显然会破坏转换它的位,因为它找不到正确的字段名称。

我知道这是一个奇怪且非常具体的问题,但有人对我如何成功解决这个问题有任何想法吗?

我目前的 PHP 代码(出于隐私考虑,去掉了顶部的参数):

<?php session_start();

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Results</title>
</head>

<body>

<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = '';
   
// Your email address. This is where the form information will be sent.
$emailadd = '';

// Where to redirect after form is processed.
$url = '';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';
  
$time = time();

// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
$securimage = new Securimage();
  
if ($securimage->check($_POST['captcha_code']) == false) {
   
   // handle the error accordingly with your other error checking
   // or you can do something really basic like this
   die('The code you entered was incorrect.  Go back and try again.');
}
foreach ($_POST as $key => $value)
{
   if ($key != "captcha_code"){
       $j = strlen($key);
       if ($j >= 40){echo "Name of form element $key cannot be longer than 39 characters";die;}
       $j = 40 - $j;
       for ($i = 1; $i <= $j; $i++)
       {$space .= ' ';}
       $value = str_replace('\n', "$line", $value);
       $conc = "{$key}:$space{$value}$line";
       $text .= $conc;
       $space = ' ';
   }
}
$text .= 'END OF APPLICATION';

mail($emailadd, $subject, $text, 'From: ');
echo '<script>alert("Application successfully submitted.");</script>';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>

</body>
</html>

以下是电子邮件的外观,我需要它是这样的,但包含所有字段,无论它们是否具有值:

【问题讨论】:

  • 不要向浏览器询问您已有的信息。如果请求包含组成的字段怎么办,您也会发送它们吗?

标签: php html


【解决方案1】:

创建一个数组,列出电子邮件中应包含的所有字段。然后不是循环遍历$_POST,而是遍历该数组。 $_POST字段填了就显示,否则显示空值。

$fields = ['ReasonForReferral', 'FirstName', 'MiddleName', ...];
foreach ($fields as $field) {
    $conc .= "$field: " . (isset($_POST[$field]) ? str_replace($_POST[$field], "\n", $line) : '') . $line;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 2022-09-27
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    相关资源
    最近更新 更多