Send two inputs for verification, ajax and php!

Asked

Viewed 97 times

0

Good day, late and night. I have a problem sending two values with ajax, checking these values, to see if they already exist in the bd and finally returns a message telling if it exists or not! the code that worked by sending only 1 value is this:

<input readonly maxlength="10" required class="inp_editar" type="text" name="fone_tel" id="fone"/>
                        <script language="javascript">
    var user_us = $("#fone");
    $("#resposta").hide();
        user_us.blur(function() { 
            $.ajax({ 
                url: 'verifica_cont.php', 
                type: 'POST', 
                data:{"fone" : user_us.val()}, 
                success: function(data) { 
                console.log(data); 
                data = $.parseJSON(data); 
                $("#resposta").text(data.user_us);
                $("#resposta").fadeIn();
            } 
        }); 
    }); 
</script>

And the verifica_cont.php

<?php
#Verifica se tem um email para pesquisa
include('../../connect/config.php');
if(isset($_POST['fone'])){ 
    if(empty($_POST['fone']) || $_POST['fone'] == "_____-____" || $_POST['fone'] == "____-____"){
    echo json_encode(array('user_us' => 'Preencha o contato do Contribuinte'));     
    }else{
    #Recebe o Email Postado
    $userPostado = $_POST['fone'];

    #Conecta banco de dados
    $sql = mysql_query("SELECT * FROM n_contribuintes_tel WHERE fone_tel = '{$userPostado}' ") or print mysql_error();

    #Se o retorno for maior do que zero, diz que já existe um.
    if(mysql_num_rows($sql)>0) 
        echo json_encode(array('user_us' => 'Telefone já cadastrado, tente outro!')); 
    else
        echo json_encode(array('user_us' => 'Telefone valido!' )); 
    }
}
?>

The one above works perfectly, I have tried several ways, but I have no idea how to do with two inputs, the last attempt to send the two values was this:

<input readonly maxlength="10" required class="inp_editar" type="text" name="fone_tel" id="fone"/>
                <input  maxlength="10" required class="inp_editar" type="text" name="ramal_tel" id="ramal_tel"/>
                        <script language="javascript">
    var user_us = $("#fone");
     var user_us_ramal = $("#ramal_tel"); 
    $("#resposta").hide();
        user_us.blur, user_us_ramal.blur(function() { 
            $.ajax({ 
                url: 'verifica_cont.php', 
                type: 'POST', 
                data:{"fone" : user_us.val(), "ramal_tel" : user_us_ramal.val()}, 
                success: function(data) { 
                console.log(data); 
                data = $.parseJSON(data); 
                $("#resposta").text(data.user_us);
                $("#resposta").fadeIn();
            } 
        }); 
    }); 
</script>

verifica_cont.php

<?php
#Verifica se tem um email para pesquisa
include('../../connect/config.php');
if(isset($_POST['fone'])){ 
    if(empty($_POST['fone']) || $_POST['fone'] == "_____-____" || $_POST['fone'] == "____-____"){
    echo json_encode(array('user_us' => 'Preencha o contato do Contribuinte'));     
    }else{
    #Recebe o Email Postado
    $userPostado = $_POST['fone'];
    $userPostado_s = $_POST['ramal_tel'];

    #Conecta banco de dados
    $sql = mysql_query("SELECT * FROM n_contribuintes_tel WHERE fone_tel = '{$userPostado}' and ramal_tel = '{$userPostado_s}'") or print mysql_error();

    #Se o retorno for maior do que zero, diz que já existe um.
    if(mysql_num_rows($sql)>0) 
        echo json_encode(array('user_us' => 'Telefone já cadastrado, tente outro!')); 
    else
        echo json_encode(array('user_us' => 'Telefone valido!' )); 
    }
}
?>

But it didn’t work either! 

If anyone can help me, thank you!

  • 1

    What exactly didn’t work? On which line of error?

  • <script language="javascript"> var user_us = $("#fone"); $("#reply"). Hide(); user_us.Blur(Function() { $.ajax({ url: 'verifica_cont.php', type: 'POST', data:{"fone" : user_us.val()}, Success: Function(data) { console.log(data); data = $.parseJSON(date); $("#response"). text(data.user_us); $("#reply"). fadein(); } }); }); </script> I could not send the value of two inputs for verification, I can only send #fone @Andrewribeiro

  • mysql_query is deprecated in versions higher than mysql 5, plus you are allowing sql Injection in your phone query. I could easily erase all phones from your bank. using your input, with $userPostado = "' or 1';&#xA; DELETE * FROM n_contribuintes_tel where 1;-- '";

  • I recommend using mysqli_* or PDO, and treat the query with placeholders.

  • Starts error in this Blur method... Do not use Blur like this: user_us.blur, is user_us.blur(). And that comma is weird.

  • Change <script language="javascript"> for <script type="text/javascript">.

  • Also, you do not need to parseJSON if you are sending ajax, just request the type in the object dataType: 'json',

Show 2 more comments

1 answer

0


$(function() {

var user_us = $("#fone"), 
    user_us_ramal = $("#ramal_tel"); 
    $("#resposta").hide();
        $("#fone,#ramal_tel").blur(function() { 
            var valA = user_us.val(), valB = user_us_ramal.val();

            $.ajax({ 
                url: 'verifica_cont.php', 
                type: 'POST',
                dataType:'json',                
                data: {"fone" : valA, "ramal_tel":valB}, 
                success: function(data) { 
                console.log(data); 
                $("#resposta").html(data.fone+'<br>'+data.ramal_tel)
                    .fadeIn();
            } 
        }); 
    }); 

});

See working on JSFIDDLE.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.