0

I am grabbing data dynamically in PHP and placing it in the name attribute of the input type="text". And based on a condition on the returned data, type="checkbox" element is given a checked attribute. They display as intended on the first page load.

But even if I 'casually' enter a value in these input fields or change the checkbox status and refresh the page, without submitting the form, these values are saved in the browser somehow and they are displayed rather than the dynamically grabbed data. To tackle this I turned the autocomplete attribute on for text. Works well, but now the problem is that the dynamic data too is not populated. It leaves the input fields blank.

And also for the checkbox field, even though I have the checked attribute turned on based on a condition on grabbing data from backend, if I uncheck it and refresh page (without submitting the form) it remains unchecked. They work well on a hard-refresh. What could be the issue? Thank you.

<div class="timing">

<?php
foreach ($value as $ky => $val) : ?>

    <?php
        if( strstr( $ky, 'AM' ) ) : ?>
            <input name="<?php echo $ky; ?>" type="number" value="<?php echo $val; ?>" class="am" placeholder="am"/>
            <?php 
            elseif( strstr( $ky, 'PM' ) ) :?>
                <input name="<?php echo $ky; ?>" type="number" value="<?php echo $val; ?>" class="pm" placeholder="pm"/> 
    <?php endif; endforeach; ?>
    
</div>
    
<?php 
foreach ($value as $ky => $val) :
    if( strstr( $ky, 'Closed' ) && !empty($val) ) :?>
        <input name="<?php echo $ky; ?>" type="checkbox" class="closed_active closed" checked/>   
<?php 
elseif( strstr( $ky, 'Closed' ) )  : ?>
<input name="<?php echo $ky; ?>" type="checkbox" class="closed"/>   
<?php endif; endforeach; ?>
Mr.Coder
  • 347
  • 1
  • 10
  • Does this happen also with [**hard refresh**](https://bacreative.com.au/how-to-hard-refresh-browser-and-clear-cache-using-windows-or-linux/#:~:text=Hold%20the%20Ctrl%20(Control)%20key,key%2C%20click%20the%20Refresh%20button.)? – medilies Feb 28 '22 at 18:58
  • @medilies Yeah works on hard refresh. – Mr.Coder Feb 28 '22 at 19:01
  • sorry I'm a bit confused. do you mean that the problem doesn't happen when you hard refresh ? – medilies Feb 28 '22 at 19:10
  • 1
    Does that really work? `` Because thats NOT valid PHP syntax I would almost bet the ranch on that – RiggsFolly Feb 28 '22 at 19:14
  • 1
    Most, if not all, browsers will save the form input on refresh, even with autocomplete off. That's just a feature of the browser. A hard refresh clears the page from the browser cache, so the input disappears. – aynber Feb 28 '22 at 19:15
  • **ALWAYS Always always!!!** Add [error reporting](http://stackoverflow.com/questions/845021/) to the top of your file(s) _while testing_ right after your opening PHP tag for example. Even if you are developing on a server configured as LIVE you will now see any errors. ` – RiggsFolly Feb 28 '22 at 19:17
  • @RiggsFolly those a and b values were given just for reference. And if you are referring to the ':' then it's modern PHP in case you weren't aware. ido it all the time.So do me a favour and remove that down vote if you have done it. – Mr.Coder Feb 28 '22 at 19:20
  • @medilies, yes you understand correctly. If i change the input values or the checkbox status and do a normal page refresh, these casually entered values still show up and it appears that PHP doesn't grab the values again until I hard refresh the page. – Mr.Coder Feb 28 '22 at 19:23
  • I am aware how to code an IF, _new and old method_, but variables called `a` and `b` are not legal unless they are defined variable. I think I have said this to you before, WE SEE ONLY WHAT YOU SHOW US, we will debug what you show us. So show us THE READL CODE – RiggsFolly Feb 28 '22 at 19:26
  • @RiggsFolly you cannot re-create the data grabbing in this scenario. Why are you complicating this? My matter relates to caching issue. Anyway, if you'd still not remove the down-vote, so be it. – Mr.Coder Feb 28 '22 at 20:54
  • In order for us all to understand this situation I think you need to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) so that we can all see it in action. As it stands your question is rather more Essay than Programming related, hence the DV. – RiggsFolly Feb 28 '22 at 21:42
  • @RiggsFolly I tried the best I could with my level of english to explain the problem. If still that is not understandable then you are right I cant do anything else. Not to forget, the other guys commenting got exactly what I am trying to explain. So I guess the problem is with your understanding rather than my literature. You may keep your DV as it's free for you and you could enjoy throwing it around at lesser experienced devs. Very motivating and trust instilling for a platform like this indeed. – Mr.Coder Feb 28 '22 at 21:54
  • If you use Code and not Language, we will all understand it. – RiggsFolly Feb 28 '22 at 21:55
  • The DV is NOT supposed to de-motivate, it is supposed to police the site to keep its content good. I notice that you are not getting a crowd of people offering answers. So maybe, just maybe I am actually trying to assist you into asking a question in a way that might garner some answers – RiggsFolly Feb 28 '22 at 21:57
  • @RiggsFolly Thank you for the effort but I've seen way too many poorly written questions with abominable grammar being up-voted thousand times. Anyway, I don't want to push this further as I am already frustrated with the code where I am stuck. Have it your way. – Mr.Coder Feb 28 '22 at 22:05
  • So why not simply show us your code – RiggsFolly Feb 28 '22 at 22:10
  • @RiggsFolly Please check the original post. I've included the code. – Mr.Coder Mar 01 '22 at 08:40
  • What browsers are you using for testing – RiggsFolly Mar 01 '22 at 12:42
  • @RiggsFolly Firefox 97.0.1 – Mr.Coder Mar 04 '22 at 08:55
  • Did you read @aynber comment. – RiggsFolly Mar 04 '22 at 10:32
  • Here are some questions that might be of some help: [Firefox keeps form data on reload](https://stackoverflow.com/questions/7377301/firefox-keeps-form-data-on-reload), [Preventing Firefox from remembering the input value on refresh with a meta tag](https://stackoverflow.com/questions/2486474/preventing-firefox-from-remembering-the-input-value-on-refresh-with-a-meta-tag) – aynber Mar 04 '22 at 12:50

0 Answers0