lovephp Posted July 15, 2016 Share Posted July 15, 2016 this following code basically does is subtract 1 form the total value <script> $(function () { $('#featured').change(function () { var currentValue = parseInt($('#amount').text()); var newValue = currentValue + ($(this).prop('checked') ? -1 : 1); $('#amount').text(newValue); }); }); </script> and you can see here inside the span there is a value 5 <input type="checkbox" id="featured" name="featured" <?php echo $checked;?> /> (<span id="amount">5</span>) Featured the issue i face is that when i submit form yes the checkbox stays checked but the subtracted value from the span gets back to 5 again all i want is the value not to changed once subtracted when box is checked and submitted form but if unchecked then yes it should again go back to 5 any help here? Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/ Share on other sites More sharing options...
requinix Posted July 15, 2016 Share Posted July 15, 2016 I don't get why you expect it to show anything other than 5. It's what you put into the HTML. Directly. No PHP code, no variables, no Javascript to update the value on page load... Why do you think it should be anything other than "5"? Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/#findComment-1534549 Share on other sites More sharing options...
lovephp Posted July 16, 2016 Author Share Posted July 16, 2016 I don't get why you expect it to show anything other than 5. It's what you put into the HTML. Directly. No PHP code, no variables, no Javascript to update the value on page load... Why do you think it should be anything other than "5"? well the 5 was just and example, it could be anything like 1, 3, 6 ot 9 between these 4 numbers and yes it would be fetched from mysaql database like (<span id="amount"> <?php echo $amount; ?> </span>) Featured but the issue if how to keep the subtracted value 1 from any of those above numbers stays same even on form submit? as of now 5 for example when check the box becomes 4 but when submit form it goes back to 5 but then when uncheck the checkbox the 5 becomes 6. how to achieve this to not change? once checked and submitted until its unchecked Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/#findComment-1534554 Share on other sites More sharing options...
requinix Posted July 16, 2016 Share Posted July 16, 2016 well the 5 was just and example, it could be anything like 1, 3, 6 ot 9 between these 4 numbers and yes it would be fetched from mysaql database like ( <?php echo $amount; ?> ) FeaturedOkay, great, so now we've established that the code you're posting is not the actual code you have. but the issue if how to keep the subtracted value 1 from any of those above numbers stays same even on form submit? as of now 5 for example when check the box becomes 4 but when submit form it goes back to 5 but then when uncheck the checkbox the 5 becomes 6. how to achieve this to not change? once checked and submitted until its unchecked The first step is to post your actual code. Also, if you need to know three states for the input (default, checked, unchecked) then a mere checkbox is not enough because all it can tell you is whether it was checked. You'll have to combine that with knowledge about whether the form was submitted. Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/#findComment-1534555 Share on other sites More sharing options...
lovephp Posted July 16, 2016 Author Share Posted July 16, 2016 no no you are getting me wrong it is the actual code you are misunderstanding me. but anyways here is the part the php $amopunt = 5; if(isset($_POST['featured'])){ $checked = "checked"; } the html <input type="checkbox" id="featured" name="featured" <?php echo $checked;?> /> (<span id="amount"> <?php echo $amount; ?> </span>) Featured this is the actual code i have no gone into database yet but once this is sorted then ill get results for <span id="amount"> <?php echo $amount; ?> </span> from database Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/#findComment-1534557 Share on other sites More sharing options...
kicken Posted July 16, 2016 Share Posted July 16, 2016 If you want the change to persist across page loads, then you need to store and retrieve the value from somewhere, such as a database. You can't "sort this out" until you do the database work first. Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/#findComment-1534558 Share on other sites More sharing options...
lovephp Posted July 16, 2016 Author Share Posted July 16, 2016 Not even by setting cookie? Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/#findComment-1534559 Share on other sites More sharing options...
kicken Posted July 16, 2016 Share Posted July 16, 2016 A cookie would limit the change to only a single browser, only work if they have cookies enabled, and get reset if they ever cleared their cookies. If that's fine for what you need then sure, use a cookie. Use something like JS-Cookie to get/set your cookie. If you need the change to be more permanent then you're back to using a database. Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/#findComment-1534560 Share on other sites More sharing options...
lovephp Posted July 16, 2016 Author Share Posted July 16, 2016 Cookie would do for me bit i haven't clue how could do that, would appreciate if you could help me with that how set subtraced value to cookie when checked amd also when submit but if unchecked clears cookie and goes back to the whatever value was there? Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/#findComment-1534564 Share on other sites More sharing options...
Muddy_Funster Posted July 18, 2016 Share Posted July 18, 2016 I always find html localStorage ( https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API ) is a quick, easy (and yeah ok, kinda dirty) way to have JS persistence, sessionStorage would probably be better for your use case though. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301487-check-value-stays-unchanged-even-after-form-submit/#findComment-1534587 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.