• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript if anyone can help how to prevent duplicated data. currently it shows error msg but still inserts new row

sandman141

New Coder
JavaScript:
if anyone can help how to prevent duplicated data. currently it shows error msg but still inserts new row
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style>
      .errorMsg {
        background-color: rgb(176, 8, 33);
        display: none;
      }
    </style>
  </head>
  <body>
    <table id="tablepress-2" class="tablepress tablepress-id-2">
      <caption
        style="
          caption-side: bottom;
          text-align: left;
          border: none;
          background: none;
          margin: 0;
          padding: 0;
        "
      >
        <a
          href="https://jonathanf.aunmedia.org/shookibalonim/wp-admin/admin.php?page=tablepress&amp;action=edit&amp;table_id=2"
          rel="nofollow"
          >עריכה</a
        >
      </caption>

      <tbody class="row-hover">
        <tr class="row-1">
          <td class="column-1">שם</td>
          <td class="column-2">שם משפחה</td>
          <td class="column-3">תעודת זהות</td>
          <td class="column-4">דמות אהובה</td>
          <td class="column-5">מוטו לחיים</td>
        </tr>
      </tbody>
    </table>
    <div class="elementor-form-fields-wrapper elementor-labels-above">
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-name elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[name]"
          id="form-field-name"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="שם"
        />
      </div>
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-field_4ee346e elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[field_4ee346e]"
          id="idoflastname"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="שם משפחה"
        />
      </div>
      <div
        class="elementor-field-type-number elementor-field-group elementor-column elementor-field-group-email elementor-col-100 elementor-field-required"
      >
        <input
          type="number"
          name="form_fields[email]"
          id="form-field-email"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="תעודת זהות"
          required="required"
          aria-required="true"
          min=""
          max=""
        />
      </div>
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-field_4efd44c elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[field_4efd44c]"
          id="form-field-field"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="דמות אהובה"
        />
      </div>
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-field_b2f2f3d elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[field_b2f2f3d]"
          id="form-field-field_b2f2f3d"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="מוטו לחיים"
        />
      </div>
      <div
        class="elementor-field-group elementor-column elementor-field-type-submit elementor-col-100 e-form__buttons"
      >
        <button
          type="submit"
          id="myFunction"
          class="elementor-button elementor-size-sm"
        >
          <span>
            <span class="elementor-button-icon"> </span>
            <span class="elementor-button-text">שליחה</span>
          </span>
        </button>
      </div>
    </div>
    <div id="result"></div>
    <p id="errorLastName" class="errorMsg"></p>

    <script>
      let clickerButton = document.getElementById("myFunction");
      clickerButton.addEventListener("click", function () {
        let nameValue = document.getElementById("form-field-name").value;
        let lastNameValue = document.getElementById("idoflastname").value;
        let inemailCell = document.getElementById("form-field-email").value;
        let favorite = document.getElementById("form-field-field").value;
        let moto = document.getElementById("form-field-field_b2f2f3d").value;

        let firstCell = document.querySelectorAll("td");
        let formValid = false; // Declare formValid only once outside the loop

        for (let index = 5; index < firstCell.length; index += 5) {
          let currentCellContent = firstCell[index].innerText;
          let nextCellContent = firstCell[index + 1].innerText;
          let emailCellContent = firstCell[index + 2].innerText;

          if (
            currentCellContent === nameValue &&
            nextCellContent === lastNameValue &&
            emailCellContent === inemailCell
          ) {
            formValid = false;
            console.log("error first name");
            // הודאת שגיאה של משתמש כבר קיים
            document.getElementById("errorLastName").innerText = "name already exist";
            document.getElementById("errorLastName").style.display = "block";
          } else if ((formValid.innerText = true)) {
            document.getElementById("errorLastName").style.display = "none";
          }
        }
        formValid = true;
        let newRow = document.createElement("tr");

        let nameCell = document.createElement("td");
        nameCell.textContent = nameValue;

        let lastNameCell = document.createElement("td");
        lastNameCell.textContent = lastNameValue;

        let emailCell = document.createElement("td");
        emailCell.textContent = inemailCell;

        let field1Cell = document.createElement("td");
        field1Cell.textContent = favorite;

        let field2Cell = document.createElement("td");
        field2Cell.textContent = moto;

        let asd = 5;

        let asd1 = 6;

        newRow.appendChild(nameCell);
        newRow.appendChild(lastNameCell);
        newRow.appendChild(emailCell);
        newRow.appendChild(field1Cell);
        newRow.appendChild(field2Cell);

        document.querySelector(".tablepress tbody").appendChild(newRow);
      });
    </script>
  </body>
</html>
 
JavaScript:
if anyone can help how to prevent duplicated data. currently it shows error msg but still inserts new row
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style>
      .errorMsg {
        background-color: rgb(176, 8, 33);
        display: none;
      }
    </style>
  </head>
  <body>
    <table id="tablepress-2" class="tablepress tablepress-id-2">
      <caption
        style="
          caption-side: bottom;
          text-align: left;
          border: none;
          background: none;
          margin: 0;
          padding: 0;
        "
      >
        <a
          href="https://jonathanf.aunmedia.org/shookibalonim/wp-admin/admin.php?page=tablepress&amp;action=edit&amp;table_id=2"
          rel="nofollow"
          >עריכה</a
        >
      </caption>

      <tbody class="row-hover">
        <tr class="row-1">
          <td class="column-1">שם</td>
          <td class="column-2">שם משפחה</td>
          <td class="column-3">תעודת זהות</td>
          <td class="column-4">דמות אהובה</td>
          <td class="column-5">מוטו לחיים</td>
        </tr>
      </tbody>
    </table>
    <div class="elementor-form-fields-wrapper elementor-labels-above">
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-name elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[name]"
          id="form-field-name"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="שם"
        />
      </div>
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-field_4ee346e elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[field_4ee346e]"
          id="idoflastname"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="שם משפחה"
        />
      </div>
      <div
        class="elementor-field-type-number elementor-field-group elementor-column elementor-field-group-email elementor-col-100 elementor-field-required"
      >
        <input
          type="number"
          name="form_fields[email]"
          id="form-field-email"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="תעודת זהות"
          required="required"
          aria-required="true"
          min=""
          max=""
        />
      </div>
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-field_4efd44c elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[field_4efd44c]"
          id="form-field-field"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="דמות אהובה"
        />
      </div>
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-field_b2f2f3d elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[field_b2f2f3d]"
          id="form-field-field_b2f2f3d"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="מוטו לחיים"
        />
      </div>
      <div
        class="elementor-field-group elementor-column elementor-field-type-submit elementor-col-100 e-form__buttons"
      >
        <button
          type="submit"
          id="myFunction"
          class="elementor-button elementor-size-sm"
        >
          <span>
            <span class="elementor-button-icon"> </span>
            <span class="elementor-button-text">שליחה</span>
          </span>
        </button>
      </div>
    </div>
    <div id="result"></div>
    <p id="errorLastName" class="errorMsg"></p>

    <script>
      let clickerButton = document.getElementById("myFunction");
      clickerButton.addEventListener("click", function () {
        let nameValue = document.getElementById("form-field-name").value;
        let lastNameValue = document.getElementById("idoflastname").value;
        let inemailCell = document.getElementById("form-field-email").value;
        let favorite = document.getElementById("form-field-field").value;
        let moto = document.getElementById("form-field-field_b2f2f3d").value;

        let firstCell = document.querySelectorAll("td");
        let formValid = false; // Declare formValid only once outside the loop

        for (let index = 5; index < firstCell.length; index += 5) {
          let currentCellContent = firstCell[index].innerText;
          let nextCellContent = firstCell[index + 1].innerText;
          let emailCellContent = firstCell[index + 2].innerText;

          if (
            currentCellContent === nameValue &&
            nextCellContent === lastNameValue &&
            emailCellContent === inemailCell
          ) {
            formValid = false;
            console.log("error first name");
            // הודאת שגיאה של משתמש כבר קיים
            document.getElementById("errorLastName").innerText = "name already exist";
            document.getElementById("errorLastName").style.display = "block";
          } else if ((formValid.innerText = true)) {
            document.getElementById("errorLastName").style.display = "none";
          }
        }
        formValid = true;
        let newRow = document.createElement("tr");

        let nameCell = document.createElement("td");
        nameCell.textContent = nameValue;

        let lastNameCell = document.createElement("td");
        lastNameCell.textContent = lastNameValue;

        let emailCell = document.createElement("td");
        emailCell.textContent = inemailCell;

        let field1Cell = document.createElement("td");
        field1Cell.textContent = favorite;

        let field2Cell = document.createElement("td");
        field2Cell.textContent = moto;

        let asd = 5;

        let asd1 = 6;

        newRow.appendChild(nameCell);
        newRow.appendChild(lastNameCell);
        newRow.appendChild(emailCell);
        newRow.appendChild(field1Cell);
        newRow.appendChild(field2Cell);

        document.querySelector(".tablepress tbody").appendChild(newRow);
      });
    </script>
  </body>
</html>
Hi there,
It looks like all you have to do is clear your text boxes after you save off the info that you are collecting from them
 
if anyone can help how to prevent duplicated data. currently it shows error msg but still inserts new row

Try this one:

HTML:
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <style>
       td{
        padding: 5px 5px;
        text-align: center;
       }
      .errorMsg {
        background-color: rgb(176, 8, 33);
        display: none;
        color: #fff;
        text-align: center;
      }
      .vis{
        display: block;
      }
    </style>
  </head>
  <body>
    <table id="tablepress-2" class="tablepress tablepress-id-2" border="1">
      <caption
        style="
          caption-side: bottom;
          text-align: left;
          border: none;
          background: none;
          margin: 0;
          padding: 0;
        "
      >
        <a
          href="https://jonathanf.aunmedia.org/shookibalonim/wp-admin/admin.php?page=tablepress&amp;action=edit&amp;table_id=2"
          rel="nofollow"
          >עריכה</a
        >
      </caption>

      <tbody class="row-hover">
        <tr class="row-1">
          <td class="column-1">שם</td>
          <td class="column-2">שם משפחה</td>
          <td class="column-3">תעודת זהות</td>
          <td class="column-4">דמות אהובה</td>
          <td class="column-5">מוטו לחיים</td>
        </tr>
      </tbody>
    </table>
    <div class="elementor-form-fields-wrapper elementor-labels-above">
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-name elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[name]"
          id="form-field-name"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="שם"
          value=""
        />
      </div>
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-field_4ee346e elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[field_4ee346e]"
          id="form-idoflastname"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="שם משפחה"
          value=""
        />
      </div>
      <div
        class="elementor-field-type-number elementor-field-group elementor-column elementor-field-group-email elementor-col-100 elementor-field-required"
      >
        <input
          type="email"
          name="form_fields[email]"
          id="form-field-email"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="תעודת זהות"
          required="required"
          aria-required="true"
          value=""
        />
      </div>
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-field_4efd44c elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[field_4efd44c]"
          id="form-field-field"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="דמות אהובה"
          value=""
        />
      </div>
      <div
        class="elementor-field-type-text elementor-field-group elementor-column elementor-field-group-field_b2f2f3d elementor-col-100"
      >
        <input
          size="111"
          type="text"
          name="form_fields[field_b2f2f3d]"
          id="form-field-field_b2f2f3d"
          class="elementor-field elementor-size-sm elementor-field-textual"
          placeholder="מוטו לחיים"
          value=""
        />
      </div>
      <div
        class="elementor-field-group elementor-column elementor-field-type-submit elementor-col-100 e-form__buttons"
      >
        <button
          type="submit"
          id="myFunction"
          class="elementor-button elementor-size-sm"
        >
          <span>
            <span class="elementor-button-icon"> </span>
            <span class="elementor-button-text">שליחה</span>
          </span>
        </button>
      </div>
    </div>
    <div id="result"></div>
    <p id="errorLastName" class="errorMsg"></p>

    <script>
      const clickerButton = document.querySelector('#myFunction'),
            errMsg = document.querySelector('#errorLastName'),
            tab = document.querySelector('#tablepress-2'),
            fields = [...document.querySelectorAll('[id^="form-"]')];
      let db = [];
      
      function dbUpdate(){
        const trs = tab.querySelectorAll('tr');
            if( trs.length == 1 ) return;
            db.length = 0;
            for( let i = 1; i < trs.length; i++ ){
                db.push( [...trs[i].querySelectorAll('td')].map( x => x.innerHTML ) );
            }
        //console.log( JSON.stringify(db) );
      }
      
      function showError(txt, t){
        errMsg.innerHTML = txt;
        errMsg.classList.toggle('vis');
        setTimeout( () => errMsg.classList.toggle('vis'), t*1000 );
      }
      
      
      
      clickerButton.addEventListener( 'click', () => {
        const formVals = fields.map( field => field.value ),
              reqFields = formVals.slice(0, 3);
        let found = 0;
      
        if( reqFields.some( n => !n.length ) ){
            showError('The first three fields can not be empty!', 1);
            fields.find( x => x.value == '' ).focus();
            return;
        }
        
        for( let field of reqFields ){
            const ind = reqFields.indexOf(field);
                  //console.log( db.map( a => a[ind] ) );
                  if( db.map( a => a[ind] ).some( z => z == field ) ){
                    fields[ind].focus();
                    showError(`The value "${field}" already exists!`, 2);
                    found++;
                    //console.log( 'stopped at index ' + ind );
                    break;
                  }
        }
        
        //console.log(found);
        
        if( !found ){
            tab.insertAdjacentHTML( 'beforeend', `<tr>${formVals.map( n => `<td>${ n.length ? n : '--' }</td>` ).join('')}</tr>` );
            dbUpdate();
            fields.forEach( a => a.value = '' );
        }
        
      });
    </script>
  </body>
</html>
 
Top Bottom