0
<script>
function refreshPage()
{
  document.forms[0].submit();
}    
</script>
<?php
 foreach($values as $value)
  {
  ?>
   <input type = "checkbox" name = "food[]" value = "<?php echo($value['dinner']);?>">
  <?php 
       } 
    ?>



<button name = "pass" onClick = "refreshPage()">refresh</button>

so I have n amount of check box (depending on how many values user has)I want to keep them the check box check after the page refresh (by clicking on refresh button). I tried searching and saw similar form but the answer did not work for me.

For example the most popular answer was:

<input type="checkbox" name="txtCheck" value="your value"
 <?php if(isset($_POST['txtCheck']))
 echo "checked='checked'"; ?> 
 /><br />

this had two problem after i hit the refresh button value would not be save until i hit it twice to save(i want it to save after one click. Also sometime it save after one click but if i hit the refresh 3 time values are lost I want it to be save no matter how many time user refresh)

the second problem was it check all of the box would be check. I only want to keep the one user has check to be save.

I looked at various other possible solution but nothing worked

so if you could please help much would be appreciated. Also I need the value to be kept. I am using the checkbox value somewhere else

Edit something like this but for my array food[]. This only works for invidiual values

<input type="checkbox" name="small" class="checkbox" <?php if 
 ($_POST['small']) echo 'checked'; ?> /> Small
7
  • 1
    The question is: why do you still want the form displayed when it is submitted?\ Commented Jun 27, 2020 at 14:01
  • i have more info/code below that is display after the user submit it. The info display base on what the user select that is why i want the form to be display Commented Jun 27, 2020 at 14:03
  • Disadvantage of submitting to the same page is you have to rebuild the whole page including the form again, testing the values to see what to display. My suggestion: read on how to post the form with Ajax. After succes you can remove the submitbutton from the first form and add your info to the same page. It's a bit of work but worth while the time. Commented Jun 27, 2020 at 14:13
  • thanks for the advice. I might try that in the future. But as of this moment I just need to figure it out using php/vanilla javascript/html. It's been bothering me for a while. So if you know how to do it or find a good solution please lmk Commented Jun 27, 2020 at 14:15
  • check edit haha found something close Commented Jun 27, 2020 at 15:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.