使用 jQuery 转换 PHP 结果并显示在输入中


using jquery to convert php results and showing in input

找不到这个问题的正确标题。 但基本上我试图向input中的使用显示消息.我不想alert结果。我试过了

if(parseInt(data) == 0) $("#status").text("Not in use").addClass("green"); 
                            else $("#status").text("Already used").addClass("red");

结果一无所获。我知道 php 有效,因为我做了一个print_r (validation_result);var_dump(validation_result); 请查看下面的代码。

.PHP

 <?php
    require("config.php");
        $validate_value = isset($_POST['value']) ? $_POST['value'] : null; 
        $validate_Year =isset($_POST['Year']) ? $_POST['year'] : null; 
        $validate_postcode  = isset($_POST['postcode']) ? $_POST['postcode'] : null; 
        $select = "SELECT * FROM customers WHERE reg_year= :reg_year 
                     AND reg_code = :reg_name AND reg_value = :reg_value  LIMIT 1";
                    $bind_bb = array('reg_year' => $validate_Year,
                                        'reg_code' => $validate_code,
                                        'reg_value' => $validate_value);

                    $validate_result = select_to_array($db_connect,$select, $index, $bind_bb);

            if (count($validate_result) == 0) {   
            echo 'Customer Does not no exist';
            }       
            else {
            echo 'Already exist, please try a different postcode';
            }       
    ?>

Jquery//我试过什么

$(function(){
    $('#value').change(function() {
    if (value== "") {
    } else {
     $.ajax({
                  type:'POST',
                  url:'insert_value.php',
                  data:{
                   validate_value:$('#value').val(),
                   validate_year:$('#year').val(),
                   validate_postcode:$('#postcode').val(), 
                  },
                  success: function (data) {
                        if(parseInt(data) == 0) $("#status").text("Not in use").addClass("green"); 
                        else $("#status").text("Already used").addClass("red");
                  }
              })
   }
});
});

HMTL

 <input type="text" name="postcode"  id="postcode"  size="12" maxlength="50" />
  <input type="text" name="year"  id="year"  size="12" maxlength="50" />
   <input type="text" name="value"  id="value"  size="12" maxlength="50" />
    <input /><div id="status"></div>

success:function中,当我执行alert(data);时,它会显示函数中的消息,具体取决于我在表中检查的内容是否已经存在。 但是当我删除alert并将其替换为上面的 if 语句时,它不会在status输入中显示消息。

做错了什么?以及我该如何改进它。谢谢。

我已经检查了您的代码,代码似乎很好。请确保您的元素中只有一个status id,它应该是唯一的。

$(function(){
   $('#value').change(function() {
    var value =$(this).val();
   if (value== "") {
   } else {
     $.ajax({
              type:'POST',
              url:'insert_value.php',
              data:{
               validate_value:$('#value').val(),
               validate_year:$('#year').val(),
               validate_postcode:$('#postcode').val(), 
              },
              success: function (data) {
                    if(parseInt(data) == 0) $("#status").text("Not in use").addClass("green"); 
                    else $("#status").text("Already used").addClass("red");
              }
          })
        }
    });
 });