Jump to content

Recommended Posts

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?

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"?

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

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; ?>  ) Featured

Okay, 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.

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

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.

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?

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.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.