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&action=edit&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>