复选框条件无法正常工作

时间:2015-03-27 15:55:43

标签: php html mysql tcpdf

我从这个函数中获取数据,现在我希望根据我的条件检查复选框,我的条件是如果我从db获得值roro将会检查是否Vessel, Containerized是的,将被检查。

$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
     $checkedYes = "checked";
} if($getDataForPDF["typeOfMove"] == "roro") {
     $checkedNo = "checked";
}

我在复选框中使用了这个条件

$html .= '<tr>';
     $html .= '<td colspan="3">';
          $html .= '<small>11. TYPE OF MOVE</small> <br />';
          $html .= $getDataForPDF["typeOfMove"];
     $html .= '</td>';

     $html .= '<td colspan="2">';
          $html .= '<small>11a. CONTAINERIZED &nbsp;&nbsp;&nbsp; (Vessel only)</small> <br/>';
          $html .= '<input type="checkbox" checked="'.$checkedYes.'" name="yes"> Yes &nbsp;&nbsp;&nbsp; <input type="checkbox" checked="'.$checkedNo.'" name="no"> No';
      $html .= '</td>';
$html .= '</tr>';

我的情况是对的,但没有在checked="checked"条件下工作。我的代码出了什么问题?

3 个答案:

答案 0 :(得分:1)

你能试试吗?

$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
     $checkedYes = "checked='checked'";
} elseif($getDataForPDF["typeOfMove"] == "roro") {
     $checkedNo = "checked='checked'";
}

同时:

$html .= '<input type="checkbox" '.$checkedYes.' name="yes"> Yes &nbsp;&nbsp;&nbsp; <input type="checkbox" '.$checkedNo.' name="no"> No';

我不确定它是否解决了您的问题,但这是您要执行的操作,以便根据您的数据库信息选中复选框。

有帮助吗?

答案 1 :(得分:1)

checked属性就像在标记末尾添加checked一样简单。例如,<input type="checkbox" name="yes" checked>将是一个复选框,其中将取消选中缺席。

以下代码可以为您解决问题:

$checkedYes = '';
$checkedNo = '';
$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
     $checkedYes = " checked";// note the space before checked
} if($getDataForPDF["typeOfMove"] == "roro") {
     $checkedNo = " checked";// note the space before checked
}


$html .= '<tr>';
     $html .= '<td colspan="3">';
          $html .= '<small>11. TYPE OF MOVE</small> <br />';
          $html .= $getDataForPDF["typeOfMove"];
     $html .= '</td>';

     $html .= '<td colspan="2">';
          $html .= '<small>11a. CONTAINERIZED &nbsp;&nbsp;&nbsp; (Vessel only)</small> <br/>';
          $html .= '<input type="checkbox" name="yes"'.$checkedYes.'> Yes &nbsp;&nbsp;&nbsp; <input type="checkbox"  name="no"'.$checkedNo.'> No';
      $html .= '</td>';
$html .= '</tr>';

JSFiddle:http://jsfiddle.net/o984L61e/

修改

尝试将顶部代码块更改为:

$checkedYes = '';
$checkedNo = '';
$getDataForPDF = getDataForPDF($bookingsId, $bookingsLettersDockReceiptId);
if($getDataForPDF["typeOfMove"] == "Vessel, Containerized") {
     $checkedYes = " checked='checked'";// note the space before checked
} 
if($getDataForPDF["typeOfMove"] == "roro") {
     $checkedNo = " checked='checked'";// note the space before checked
}

examples on tcpdf使用checked='checked'属性就像其他答案一样,但是在尝试在html字符串中连接之前必须初始化变量。如问题中所述,$checkedYes$checkedNo将被定义为未定义,因为它们是在互斥的if块中初始化的。不确定这是否有所作为,但似乎有道理。

答案 2 :(得分:0)

TCPDF的writeHTML()函数仅支持有限的HTML标记子集。有关详细信息,请查看writeHTML() documentationinput标记显然是支持 的标记。

相反,您可以使用HTML实体来表示已选中和未选中的复选框。已经有StackOverflow question提供了可以使用的实体示例。

相关问题