Hi,
Am having only one check box.

I wants to pass checkbox values through hyperlink. I needs to know, either it is an checked checkbox or an unchecked uncheckbox. Based upon checked checkbox, i carry out my remaining coding part.

echo "<br>";
	echo "<table>";
	echo "<tr><td colspan='2'><font color='blue'><b>download PDF document:</b></td></tr>";
	echo "<tr><td><b>Approve :</b><input type=checkbox name=chk value=no></td><td><a target ='_blank' href='download.php?chk=no&pid=".base64_encode($id)."'>Approve it</a></td></tr>";
	echo "</table>";

Please give me the solution to sort out my problem.thanks

Dani AI

Generated

A simple, robust pattern is to treat the checkbox as a form input (GET or POST), enable the download control only when the box is checked on the client, and always enforce the check on the server. 's JS idea is on the right track but had two practical issues: the DOM method is getElementById (not getElementByID) and the checkbox state must be tested with the element's .checked property (see HTMLInputElement.checked). Relying only on client-side checks allows easy bypass; the server must validate.

Client-side (friendly UX) example: the submit button is disabled until the checkbox is checked.

<form method="get" action="download.php">
  <label><input type="checkbox" id="approve" name="approve" value="1"> Approve</label>
  <input type="hidden" name="pid" value="<?php echo htmlspecialchars(base64_encode($id)); ?>">
  <button id="download" type="submit" disabled>Download PDF</button>
</form>

<script>
document.getElementById('approve').addEventListener('change', function() {
  document.getElementById('download').disabled = !this.checked;
});
</script>

Server-side enforcement (mandatory) — validate the checkbox parameter and the pid before sending the file. Using filter_input helps:

<?php
$approve = filter_input(INPUT_GET, 'approve', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
$pid     = filter_input(INPUT_GET, 'pid', FILTER_SANITIZE_STRING);
if ($approve) {
  // verify $pid maps to an allowed file, check permissions, then stream the file
} else {
  http_response_code(403);
  echo 'Approval required.';
  exit;
}
?>

If an anchor link must be used instead of a form, intercept the click, check .checked, prevent default when unchecked, and add the approved flag to the URL when allowed. Always verify on download.php regardless of client-side behavior. Note: base64_encode is not an access control mechanism — validate the decoded id and restrict file access server-side.

Recommended Answers

All 3 Replies

So you want to pass the values of the checkboxes into the hyperlink, don't you?
If so, then you can use Javascript like the following:

echo '<script type="text/javascript">
  function changeHREF(A_ID,ChkBOX){
    document.getElementByID(A_ID).href="download.php?chk="+ChkBOX+"&pid='.base64_encode($id).'";
  }
</script>';

And the PHP-HTML code should become like:

echo '<br>
	<table>
	<tr><td colspan="2"><font color="blue"><b>download PDF document:</b></td></tr>
	<tr><td><b>Approve :</b><input id="ChkBOX" type="checkbox" name="chk" value="no" onclick="changeHREF(\'HREF1\',\'ChkBOX\')"></td><td><a id="HREF1" target="_blank" href="download.php?chk=no&pid='.base64_encode($id).'">Approve it</a></td></tr>
	</table>';

I hope it helps.

Hi,

thanks for your coding. Problem is there :
How can i find :
checked / unchecked.

If i unchecked my checkbox means, values should not pass.(user not allowed for downloading)
if i checked my checkbox means, values should pass.(now the user are allowed for downloading)

thanks

javascript is not working...onclick

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.