0

I have one textfield type text, when I'm typing a number of a plate, if it exist, it will must show one alert, but the event keyup doesn't work.

JS

$('#mat').keyup(function(){
        var matric = $('#mat').val();
        $.ajax({type: "POST",
                     url: "compruebaVeh.php",
                     data: "matric="+matric,
                     success:function(data) {
                         alert(data);
                         if(data=='ok'){
                             alert('this car already exists');
                         }
                     }
        });  
    });

PHP

<?php

 $mat=$_POST['matric'];
 ini_set('display_errors',1); error_reporting(E_ALL);
 require('conecta.php');
 $cSQL="select matricula from vehiculos;";
 $stmt=$oConni->prepare($cSQL) or die($oConni->error);
 //$stmt->bind_param('s',$_POST['matric']);
 $stmt->execute();
 $stmt->bind_result($matricula);
 while ($stmt->fetch()) {
    if($matricula==$mat){
        echo 'ok';
    }
 }   
 $stmt->close();  
?>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
jal
  • 533
  • 1
  • 5
  • 13

5 Answers5

2

in php code, the assignment $_POST['matric']=$mat; should be reversed

$mat = $_POST['matric'];

also your script may echo ok many times depending on the data in your DB, the loop should become:

while ($stmt->fetch()) {
    if($matricula==$mat){
        echo 'ok';
        break;
    }
 }  

so now you could compare in js callback with "ok".

Akram Berkawy
  • 4,920
  • 2
  • 19
  • 27
1

I'm going to fix this so you don't hammer your backend like this is set up to do. A question on SO already deals with this

//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 1000; //time in ms, 1 second for example

var licenceSearch = $("#mat");

//on keyup, start the countdown
licenceSearch.keyup(function () {
    clearTimeout(typingTimer);
    if (licenceSearch.val) {
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    }
});

//user is "finished typing," do something
function doneTyping() {
    alert(licenceSearch.val());

    // ajax stuff can happen here.
}
Community
  • 1
  • 1
Drew Larson
  • 710
  • 7
  • 12
0

You'll want to use the new Promise interface JQuery method, like so:

$('#mat').on('keyup', function (event) {

    var matric = $(event.target).val();

    var response = $.post("compruebaVeh.php", {data: matric});

    response.done(function (data) {
        alert(data)
    });

    response.fail(function () {
        alert('error')
    });

});

Here is a working keyup fiddle example: http://fiddle.jshell.net/vHTZN/

Slukehart
  • 1,037
  • 5
  • 15
  • Sorry, was trying to give some immediate feedback - meant to direct to the new promise interface. – Slukehart Mar 12 '13 at 17:39
  • Right, that isn't a bad idea, but there's nothing wrong with the way he is currently binding his event or handling his ajax request. The methods he is using aren't depreciated or superseded. They're shorthand methods. – Kevin B Mar 12 '13 at 17:41
  • On that, we will have to disagree :) – Slukehart Mar 12 '13 at 17:45
0

Make an exit statement in php code and try again.

while ($stmt->fetch()) {
    if($matricula==$mat){
        echo 'ok';
    }
 }  
exit; 
Justin John
  • 9,223
  • 14
  • 70
  • 129
0

in the most cases there id #mat is defined more than one time in the html code, also the keyup might work, but the url may not be correct, and it will not trigger the success branch. Other than that i see $_POST['matric']=$mat; wich isn't quite right. Probably you wantedd $mat = $_POST['matric']. You can check using firebug console what really happens with the javascript and what the php script returns

Andrei Stanca
  • 908
  • 2
  • 11
  • 26
  • then see what is the response, and if it is 'ok' , you should check for javascript problems. i gave some possible pointers, not having the whole picture (more code related to this) is hard to give an exact response – Andrei Stanca Mar 12 '13 at 17:41