0

I'm getting data from this function, now I want checked checkboxes based on my condition and my condition is if I got value roro from db no will be checked if I got Vessel, Containerized yes will be checked.

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

and I'm using this condition in checkboxes

$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>';

my condition is right but not working on checked="checked" condition. What is wrong in my code?

rjensen
  • 28
  • 5
user3833682
  • 263
  • 5
  • 20

3 Answers3

1

Could you try?

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

Along with:

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

I'm not sure it'd solve your problem but that is what you want to do in order the check the checkboxes accordingly to your database info.

Does it help?

D4V1D
  • 5,805
  • 3
  • 30
  • 65
1

The checked property is as simple as adding checked at the end of the tag. For example <input type="checkbox" name="yes" checked> would be a checked box where the absence of checked would be unchecked.

The following code should solve the problem for you:

$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/

Edit

Try changing the top code block to:

$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
}

The examples on tcpdf use the checked='checked' property just like the other answers had, but you must initialize the variables before you try to concatenate in the html string. As written in the question, either $checkedYes or $checkedNo will be undefined since they are being initialized in mutually exclusive if blocks. Not sure if this will make a difference but it seems plausible.

rjensen
  • 28
  • 5
0

TCPDF's writeHTML() function only supports a limited subset of HTML tags. Check the writeHTML() documentation for details. The input tag is apparently one that is not supported.

Instead, you could use HTML entities to represent the checked and unchecked checkboxes. There's already a StackOverflow question that gives examples of possible entities you could use.

Community
  • 1
  • 1
Brian Showalter
  • 4,321
  • 2
  • 26
  • 29