I have a form, and before the form is submitted I want to validate if the nome_carro
field exists the same in the database. To do this validation I'm using javascript.
In the form I have the following:
<form method="post" action="" name="add_form" onsubmit="return validateForm()">
<input type="text" name="nome_carro" required="">
.
.
.
</form>
And in the javascript the following:
<script>
function validateForm()
{
var x=document.forms["add_form"]["nome_carro"].value;
<?php
$nome_carro = "VARIAVEL X, NO JAVASCRITP";
//fazer query, mas para isso preciso da variavel $nome_carro preenchida
?>
var nome_carro_bd = <?php $valor_retornado_da_consulta?>;
if(x == nome_carro_bd)
{
alert("Por favor, insira outro nome na viatura. O nome que colocou já existe.");
return false;
}
}
</script>
To do the validation in javascript, I have to query the DB (using php) to check if the name already exists. For this I need to match the variable x
to a variable php to be able to do the query. How can I do this?