Match javascript variable to a variable in php

2

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?

    
asked by anonymous 09.04.2014 / 16:33

1 answer

4

You will need to use Ajax to perform a query and return whether or not this car exists in the database. You can not do it the way you want it because the page has already been loaded.

Take a look at this link link

    
09.04.2014 / 17:05