Add if no same mail is available in the textarea field

Hello,
I made a video just because I can’t tell.

add if no same mail is available in the textarea field
Video see: https://antenfiyati.com/checkbox.mp4

<script>

$(document).on('change', '#emailsec', '.emailsec', function(){

      if ($(this).is(':checked')) {

          $('.output').val($('.output').val() + $(this).val() + ",");

      } else {

          currentVal = $('.output').val();

          currentVal = currentVal.replace($(this).val() + ",", '');

          $('.output').val(currentVal);

      }

  });

</script>

Without (the use of) pagination i should just walk through each checkbox on the page and took its value that then holds the mailaddress and added it to a string, But if you want the user to build a selection over multiple pages then the situation is different. In this case you could make an AJAX call each time the state of a checkbox changes and keep a list of selected checkboxes in the SESSION. There is also a way to keep the selected items in a GET variabele but this manner is a bit ugly, you would get something as

https://www.domain.com/list/of/something?page=2&selected=%5B2%2C3%2C4%2C6%2C8%5D

Pagination with Ajax required, Because there are other options, page should not be refreshed
Priority problem:
1st. If there is the same mail in the textarea field, no insertion, Is this possible?

no not save without the help of underlying data. think about a JSON array

Is there a question? From the video it seems to work as designed?

check whether there is the same mail in the altaraf textarea field

And it appeared from the video, that it happening?

I think Adem want to prevent double entries.

Yesssss, I want to block double entries

I see it now. You are only checking if the element is checked, not if it isn’t, the else may or may not work. Try this.

<script>
$(document).on('change', '#emailsec', '.emailsec', function(){
      if ($(this).is(':checked')) {
          $('.output').val($('.output').val() + $(this).val() + ",");
      } else if ($(this).prop("checked") == false)
          $('.output').val($('.output').val().replace($(this).val() + ",", ''));
      }
  });
</script>

If it doesn’t work, I would serialize the text into an array and do a proper search for the value.

No Adding Mail, This code never worked

So, why does it need to be in the box anyway? When you submit the form it will get all checked inputs and only send them anyway. Why have them in another field as well?

I’ve updated the video and you can see it again

Checking in textarea field, no problem here,

When the user sees that the previously selected boxes are empty due to the page change, the user selects the boxes again and the second of the same mail is added to the textarea field.

No problem if the user doesn’t re-select the boxes he previously selected, if the user selects the box again, two of the same mail will be added.

Problem 1: Adding second mail
Problem 2: mail is not removed when the page changes

I would use an array.

When the checkbox is checked, add it to the array. When it is unchecked remove it. Every time the array changes, have it populate the text field as a visual, but don’t actually use that text field as the data sent to be processed.

I do not know what to do, can you help me?

What other choice is there?

Are you using ajax for the form submission? You can send it directly without needing the form at all.

var emails = [];

$(document).on('change', '#emailsec', '.emailsec', function(){
      if ($(this).is(':checked')) {
          emails.push($(this).val());
      } else if ($(this).prop("checked") === false)
          var r = emails.indexOf($(this).val());
          emails.splice(r, 1);
      }

      $('.output').val(emails.join(', ');
  });

Lastly, I see you are doing this,
#emailsec’, '.emailsec’
Do the checkboxes have the same id?

Yes ajax,
Checkbox, Admin group OR members group OR select members and subject OR (optional file attachment) and Message

Yes, <input type=“checkbox” class=“emailsec” id=“emailsec” value="<?php echo $row['firma_emaili']; ?>

I ran the above code but nothing changed

Solves this problem

var emails = [];

$(document).on('change', '#emailsec', '.emailsec', function(){

      if ($(this).is(':checked')) {

        if( emails.indexOf($(this).val()) == -1 ){

          emails.push($(this).val());

        }

      } else if ($(this).prop("checked") === false){

          var r = emails.indexOf($(this).val());

          emails.splice(r, 1);

      }

      $('.output').val(emails.join(', '));

  });
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service