jquery - How can I retrive data from db with ajax and php?

84

I'm trying to check if a field is just present into db before submit a form.

So I add the keyup event to that field to get data fromdb withajax.

So where I have the form I add this code:

$(document).ready(function (){
    $("#matricola").keyup(function () {
         $.ajax({
             type:"get",
             url: "getMatricolaAjax.php",
             data: {'type':'user', 'matricola':$("#matricola").val()},
             dataType: "text",
             success: function(result){
                console.log("OK");
                $("#matricola").val("");
                alert("Matricola "+ result +" già presente!!");
             },
             error: function(){
                console.log("KO");
             }
           });
      });
  });

And this is mygetMatricolaAjax.php:

<script src='js/jquery-2.1.4.js' type="text/javascript"></script>
    <?php
    require_once 'config.php';
    require_once FUNCTION_PATH.'/dbFunction.php';

    if($_GET['type'] == "user"){
        $queryMatricolaMatch = 'select * from user where matricola = "'.$_GET['matricola'].'"';
    }else{
        $queryMatricolaMatch = 'select * from 150ore where matricola = "'.$_GET['matricola'].'"';
    }
    echo $queryMatricolaMatch;
    $conn = dbConnect($USERDB, $PASSWORDDB, $NAMEDB);
    $matricola = dbQueryGetResult($queryMatricolaMatch);
    dbDisconnect($conn);

    echo $matricola;

It works for half, beacause inresult I obtain allhtml code fromgetMatricolaAjax.php..

Why??

How can I get onlymatricola??

54

Answer

Solution:

Comment or removedataType: "text" and try again.

   $.ajax({
         type:"get",
         url: "getMatricolaAjax.php",
         data: {'type':'user', 'matricola':$("#matricola").val()},
         // dataType: "text", 
         success: function(result){
            console.log("OK");
            $("#matricola").val("");
            alert("Matricola "+ result +" già presente!!");
         },

or else you can use json_encode() in PHP to get data as JSON object array.

358

Answer

Solution:

You should use POST to check for value in database before submitting a form.

$(document).ready(function (){
       $("#matricola").keyup(function (e) { 
         var thevalue = $(this).val();
         $.post('getMatricolaAjax.php',{'type':'user','matricola':thevalue}, 
           function(data) { 
            console.log("OK");
            $("#matricola").val("");
            alert("Matricola "+ data +" già presente!!");
           });
         });
      });

And the php file

<?php
require_once 'config.php';
require_once FUNCTION_PATH.'/dbFunction.php';
$conn = dbConnect($USERDB, $PASSWORDDB, $NAMEDB);

if($conn->real_escape_string($_POST['type']) == "user"){
    $queryMatricolaMatch = 'select * from user where matricola = "'.$conn->real_escape_string($_POST['matricola']).'"';
}else{
    $queryMatricolaMatch = 'select * from 150ore where matricola = "'.$conn->real_escape_string($_POST['matricola']).'"';
}

$matricola = dbQueryGetResult($queryMatricolaMatch);
dbDisconnect($conn);

echo $matricola;

This should work, but I have no idea what dbQueryGetResult is supposed to do so you should post it too. Note:If you use PDO edit remove 'real_escape_string' function and use other methods of sanitization

People are also looking for solutions to the problem: php - How can I insert a value into the last database entry?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.