Jquery Ajax总是失败

时间:2014-07-01 13:06:45

标签: php jquery ajax onkeyup

我想检查人们在表单中输入文本中输入的数字是否已经在db中。

表格(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();

?>

脚本总是在ajax中失败并向我显示错误消息:&#34;错误!!!&#34;

1 个答案:

答案 0 :(得分:0)

您没有在verifica.php脚本中定义$ MySQLi。 当您通过ajax调用脚本时,它无法看到之前定义的其他变量。