I GOT 2 Questions
1) So, below is the code that will keep user's checkbox checked after user submitted the form, so that he know which one he had checked and dont need to fill again. But the question is it did not show the array, i guess is the
echo ($_POST['ao']);
I tried use print_r,but it show out all the array, i just want to echo out the specific item that user checked.Currently it just echo "Array".
2) It did now display the input type text if user is checked after submit.But before submit, it works.
function ao1Function(){document.getElementById("ao1").value = document.getElementById("ao1").disabled = false; document.getElementById("ao1").style.display = "inline-block";} function ao1Function2(){document.getElementById("ao1").value = document.getElementById("ao1").style.display = "none"; document.getElementById("ao1").disabled = true; }
<input type="checkbox" name="cbox[]" value="Basic Add To Cart,0" onclick="if(this.checked){ao1Function()}else{ao1Function2()}"
<?php if (isset($_POST['cbox']) && in_array("Basic Add To Cart,0",$_POST['cbox'])){echo 'checked';}?>>
Basic Add To Cart<div id="ao1" style='display:none;'>
<input type="text" id="ao" min="0" step="0.05" name="ao[]" onchange="force2decimals(this)" onkeypress='validate(event)' inputmode='numeric' placeholder="Amount (RM)"
value="<?php if(isset($_POST['ao'])) echo ($_POST['ao']);?>" <?php if(isset($_POST['cbox'])) echo "style='display:inline-block;'";?>></div>
1) Here's how to show only the selected check-boxes
var_dump(array_filter($_POST['ao'], function($v) {return $v == 'true';}));
2) The JavaScript part looks OK. You might need a little fix on the PHP side and it will work perfect.
before:
<?php if(isset($_POST['cbox'])) echo "style='display:inline-block;'";?>
after:
<?php if(isset($_POST['cbox'])) echo "style='display:inline-block;'"; else echo "style='display:none;'";?>