I'm using a form with 2 submit buttons. The first button generates a table with information:
if((!empty($_POST['Generate_Reports']) ? $_POST['Generate_Reports'] : null) == "Generate Reports") {
$_SESSION['begin'] = new DateTime((isset($_POST['start_date']) ? $_POST['start_date'] : null));
$_SESSION['end']= new DateTime((isset($_POST['end_date']) ? $_POST['end_date'] : null));
$_SESSION['end'] = $_SESSION['end']->modify('+1 day');
$_SESSION['interval'] = new DateInterval('P1D');
$_SESSION['daterange'] = new DatePeriod($_SESSION['begin'], $_SESSION['interval'] ,$_SESSION['end']);
echo '<table style="width:100%">
<tr>
<th>Export</th>
<th>Date Referral Submitted</th>
<th>Name of Person being Referred for Services</th>
<th>Date of Birth</th>
<th>Referral Agency</th>
<th>Date Exported</th>
<th>View Form</th>
<th>View Exported Form</th>
</tr>';
foreach($_SESSION['daterange'] as $date){
$response = $documents->search(
array("schema_id" => $schemaId, "filter" =>
array("date_of_referral" =>
$date->format("m/d/Y")
)
)
);
$documentsData = $documents->get($response["documents"]);
$_SESSION['response'] = count($response["documents"]);
for ($i = 0; $i < $_SESSION['response']; $i++) {
$documentId = isset($response["documents"][$i]) ? $response["documents"][$i] : null;
$documentIds[] = $documentId;
$documentData = isset($documentsData[$documentId]) ? $documentsData[$documentId] : null;
echo '<tr id="'.'varun'.$i.'">'
. '<td>' . '<input type="checkbox" name="export_'.$i.'" value="1" id="export_'.$i.'" checked>' . '</td>'
. '<td>' . $documentData["date_of_referral"] . '</td>'
. '<td>' . $documentData["first_name_of_person_referred_for_services"] . ' ' . $documentData["last_name_of_person_referred_for_services"] . '</td>'
. '<td>' . $documentData["date_of_birth"] . '</td>'
. '<td>' . $documentData["referring_agency"] . '</td>'
. '<td>' . (isset($_SESSION['date_of_export_'.$i]) ? $_SESSION['date_of_export_'.$i] : 'N/A') . '</td>'
. '<td>' . '<a href="#" onclick="myFunction(document.getElementById(\''.'varun'.strval($i).'\'))">View Form</a>' . '</td>'
. '<td>' . ' ' . '</td>'
. '</tr>';
}
}
echo '</table>';
$_SESSION['myArray'] = $documentIds;
}
In this line, I set a column in the row of the table to be a checkbox:
. '<td>' . '<input type="checkbox" name="export_'.$i.'" value="1" id="export_'.$i.'" checked>' . '</td>'
When the second button is called, it loops through the rows of the table and checks the checkbox in each row to see it if is checked or not, and then outputs the result.
Because this is another submit button, I needed to retain the table (because it wouldn't show since it only displayed when the first submit button is called) or else I wouldn't be able to grab the checkbox value. So I repeated the code using the $_SESSION variables:
if((!empty($_POST['Export']) ? $_POST['Export'] : null) == "Export") {
echo '<table style="width:100%">
<tr>
<th>Export</th>
<th>Date Referral Submitted</th>
<th>Name of Person being Referred for Services</th>
<th>Date of Birth</th>
<th>Referral Agency</th>
<th>Date Exported</th>
<th>View Form</th>
<th>View Exported Form</th>
</tr>';
foreach($_SESSION['daterange'] as $date){
$response = $documents->search(
array("schema_id" => $schemaId, "filter" =>
array("date_of_referral" =>
$date->format("m/d/Y")
)
)
);
$documentsData = $documents->get($response["documents"]);
$_SESSION['response'] = count($response["documents"]);
for ($i = 0; $i < $_SESSION['response']; $i++) {
$documentId = isset($response["documents"][$i]) ? $response["documents"][$i] : null;
$documentIds[] = $documentId;
$documentData = isset($documentsData[$documentId]) ? $documentsData[$documentId] : null;
echo '<tr id="'.'varun'.$i.'">'
. '<td>' . '<input type="checkbox" name="export_'.$i.'" value="1" id="export_'.$i.'" checked>' . '</td>'
. '<td>' . $documentData["date_of_referral"] . '</td>'
. '<td>' . $documentData["first_name_of_person_referred_for_services"] . ' ' . $documentData["last_name_of_person_referred_for_services"] . '</td>'
. '<td>' . $documentData["date_of_birth"] . '</td>'
. '<td>' . $documentData["referring_agency"] . '</td>'
. '<td>' . (isset($_SESSION['date_of_export_'.$i]) ? $_SESSION['date_of_export_'.$i] : 'N/A') . '</td>'
. '<td>' . '<a href="#" onclick="myFunction(document.getElementById(\''.'varun'.strval($i).'\'))">View Form</a>' . '</td>'
. '<td>' . ' ' . '</td>'
. '</tr>';
}
}
echo '</table>';
for ($i = 0; $i < $_SESSION['response']; $i++) {
$_SESSION['checked'.$i] = isset($_POST['export_'.$i]) ? $_POST['export_'.$i] : '0';
echo($_SESSION['checked'.$i]);
if($_SESSION['checked'.$i] == "1") {
$_SESSION['date_of_export_'.$i] = date('m/d/Y');
}
}
}
In these two lines of code I check whether the checkbox in each row is checked or not (checked displays value of "1", otherwise it displays "0") and then the output is displayed:
$_SESSION['checked'.$i] = isset($_POST['export_'.$i]) ? $_POST['export_'.$i] : '0';
echo($_SESSION['checked'.$i]);
Now, this is where I get a problem. To test out whether my code worked, I already set the checkboxes to be checked. Therefore, it is expected that when I click the "Export" button, it should display a value of "1". However, it showed a value of "0", but I don't understand why it is happened.
Help will be greatly appreciated. I've been stuck on this for quite some time now.
Aucun commentaire:
Enregistrer un commentaire