Stack Overflow Asked by sevenine on December 10, 2020
I understand that we can pre-fill some data in a HTML form using the URL.
For example, www.example.com/?itemname=sth
May I know would there be a similar way for me to pre-check a checkbox through the URL?
<ul class="list-group">
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" id="Tutor_subject_1" name="Maths" value="Maths"
<?php if (isset($_GET['itemname']) && $_GET['itemname'] === 'sth' ): ?>
document.getElementByName("Maths").click();
<?php endif; ?>>Maths<br>
</label>
</div>
</li>
</ul>
Thank you @jrswgtr. I have one more follow up question. The above setting is actually the filter of my result page. Your solution is workable but it simply checked the box but doesn’t pass the value of the selected items to my SQL like it usually does. May I know how could i change it?
Yes you can, do an if check on the $_GET
parameter:
<input type="checkbox"
class="form-check-input product_check"
id="Tutor_subject_1"
value="Maths"
<?php if (isset($_GET['itemname']) && $_GET['itemname'] === 'sth' ): ?>
checked
<?php endif; ?> />
Correct answer by jrswgtr on December 10, 2020
You can use php for this solution .
<?php
$item = (isset($_GET['itemname']) && $_GET['itemname'] === 'sth') ? $_GET['itemname'] : '';
?>
<ul class="list-group">
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" id="Tutor_subject_1" value="Maths" <?php if($item) {echo "checked";} ?> >Maths<br>
</label>
</div>
</li>
</ul>
Answered by Ishtiaq Ahmed on December 10, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP