I have a table that contains names and ids in my database, and I have an html form with an input that adds a name in that table, I want to warn the user whenever they try to add a name that already exists by doing a split on the input value, if the array's length is <= 2 I want only the first element, and then I use the "includes" function to check if the list of the names in the database contains the string I'm getting from the input, here's my html code:
<label for="nomConcessionnaire">Nom du concessionnaire</label>
<input type="text" class="infoConcessions" name="nom_concessionnaire" id="nomConcessionnaire" value="<?php echo $row['nom_concessionnaire']; ?>" onblur="compareNom()"/>
And my script:
function compareNom () {
var nomSaisi = document.getElementById("nomConcessionnaire").value;
var nomSplit = nomSaisi.split(' ', 3);
console.log (nomSplit);
if (nomSplit.length <= 2) {
var nomSimple = nomSplit[0];
<?php
if (!empty($concessionnaires_rows)) {
for($i = 0; $i < count($concessionnaires_rows); $i++) {
echo '
if ('.$concessionnaires_rows[$i]['nom_concessionnaire'].'.includes(nomSimple)) {
alert("Ce nom apparaît dans la liste des concessionnaires déjà existants, voulez-vous quand même l\'ajouter ?");
}
';
}
}
?>
}
}
I use a javascript function with php loop of my table rows inside.
I'm getting this error everytime:
Uncaught SyntaxError: missing ) after condition.
I can't understand why.
My Answer:
greater than 1, then the name already exists in the database.
Here is an example code using Javascript and PHP to achieve this:
HTML form:
html
Javascript code:
javascript
document.getElementById('addNameForm').addEventListener('submit', function(e) {
e.preventDefault();
var name = document.getElementById('nameInput').value;
var xhr = new XMLHttpRequest();
xhr.open('POST', 'checkName.php', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = JSON.parse(xhr.responseText);
if (response.exists) {
document.getElementById('warningMessage').innerText = 'Name already exists in the database';
} else {
document.getElementById('addNameForm').submit();
}
}
};
xhr.send('name=' + name);
});
PHP code (checkName.php):
php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
$name = $_POST['name'];
$sql = "SELECT * FROM names WHERE name = '$name'";
$result = $conn->query($sql);
$response = array();
if ($result->num_rows > 0) {
$response['exists'] = true;
} else {
$response['exists'] = false;
}
echo json_encode($response);
$conn->close();
?>
In this code, we are sending an AJAX request to the `checkName.php` file when the form is submitted. The PHP file checks if the name already exists in the database and returns a JSON response. If the name exists, we display a warning message to the user, otherwise, we submit the form to add the name to the database.
Rate this post
5 of 5 based on 8615 votesComments