-1

I would like to check if the number that the people type in input text at a form is already in db or not.

Form (index.php):

<script>
function numerovalida(numerov) {
     if (numerov < 1 || numerov > 100) { alert("Número Inválido!"); $('#numero').val(""); numero.focus(); $( "#enviar" ).prop( "disabled", true ); }
     else {
         if (!IsNum(numerov)) { alert("Número Inválido!"); $('#numero').val(""); numero.focus(); $( "#enviar" ).prop( "disabled", true ); }
         else { 

             $.ajax({
                 url: "verifica.php",
                 type: "post",
                 data: $("#inscreversorteio").serialize(),
                 success: function(data) {
                     $("#msgNumero").html(data);
                 },
                 error: function(xhr) {
                     $("#msgNumero").html("ERROR!!!");
                 }
             });
         }
     }
}
</script>

<form action="inscrever.php" method="post" name="inscreversorteio" id="inscreversorteio">
<input type="hidden" id="sorteio" value="<?php echo $_GET['id']; ?>">
<p style="text-align: center; font-weight: bold;">Informe os Dados:</p>
Nome: <input type="text" name="nome" id="nome" maxlength="25" onkeyup="checanome();" /><br />
<div style="float: left;">Número: <input type="text" name="numero" id="numero" style="width: 30px; text-align: center; align: center;" onkeyup="numerovalida(this.value);" /></div>
<div id="msgNumero" name="msgNumero" style="float: left; margin-left: 10px;"></div><p /><br />
<input type="submit" value="Inscrever" id="enviar" name="enviar" disabled />
</form>

verifica.php:

<?php

     $campo = $_POST['numero'];
     $idsn = $_POST['sorteio'];
     $consultan = 0;

     $sqlvv = "SELECT * FROM `inscritos` WHERE (`id_sorteio` = '". $idsn ."') AND (`numero` = '". $campo ."')";
     $resultvv = $MySQLi->query($sqlvv) OR trigger_error($MySQLi->error, E_USER_ERROR);
     $consultan = $resultvv->num_rows;

     $resultvv->free();

    if ($consultan > 0) {
        echo 'ERROR: Number Already in DB!';
    }
    else {
        echo "OK: the number isn't in DB yet.";
    }
    exit();

?>

The script always fails in ajax and show me the error msg: "ERROR!!!"

Dean.DePue
  • 1,013
  • 1
  • 21
  • 45
  • is there any server console error message – Manish Kumar Jul 01 '14 at 13:09
  • change your error callback to `error: function(xhr,msg) { $("#msgNumero").html("ERROR!!!"+msg);}` and it might give you a hint. – Smern Jul 01 '14 at 13:09
  • 1
    Add `console.log(xhr);` to your `error` callback. Then step through this object and see what all you have. Could be a PHP error in which case it will be reported in the returning error object. – War10ck Jul 01 '14 at 13:10
  • is there any server console error message? no =/ change your error callback to error: function(xhr,msg) { $("#msgNumero").html("ERROR!!!"+msg);} and it might give you a hint Just tell me error :P – user3790692 Jul 01 '14 at 13:13
  • Console error: POST http://localhost/sorteio/verifica.php 500 (Internal Server Error) jquery.js:8706 send jquery.js:8706 x.extend.ajax jquery.js:8136 $.ajax jquery.validate.js:1094 numerovalida VM481:10 onkeyup – user3790692 Jul 01 '14 at 13:17
  • Where is `$MySQLi` being defined? – Joe Jul 01 '14 at 13:22
  • $MySQLi is above the – user3790692 Jul 01 '14 at 13:24
  • Ok, that's your error then. An ajax call is _not_ a php include so `verifica.php` can't access previously defined variables. Define `$MySQLi` at the top of `verifica.php` and everything will work. – Joe Jul 01 '14 at 13:25
  • @Joe you're right! Thank you very much! Now all is working well =]] – user3790692 Jul 01 '14 at 13:31

1 Answers1

0

You didn't define $MySQLi in your verifica.php script. When you call a script by ajax, it can't see other variables previously definded.

silver
  • 64
  • 1
  • 1
  • 8