无法从 js 文件中获取数据

Posted

技术标签:

【中文标题】无法从 js 文件中获取数据【英文标题】:unable to get data from js file 【发布时间】:2018-04-29 23:11:16 【问题描述】:

这是从数据库 AJAX 获取数据的代码,我检查了表名和语法,但有一些问题我无法获取数据。

文件名:: ajax_check_uname.php

<?php


    $dbhandle = mysql_connect('localhost', 'root', 'root') or die("Opps some thing went wrong");
    mysql_select_db('db_new', $dbhandle) or die("Opps some thing went wrong");

    if(isset($_POST['uname']))//If a name has been submitted 
    
    $uname= mysql_real_escape_string($_POST['uname']);//Some clean up :)

    $check_for_uname = mysql_query("SELECT uname FROM `db_new`.`operators` WHERE uname='$uname'");
    //Query to check if uname is available or not 

    if(mysql_num_rows($check_for_uname))
    
    echo '1';//If there is a  record match in the Database - Not Available
    
    else
    
    echo '0';//No Record Found - uname is available 
    

    

    mysql_close($dbhandle);
    ?>

这是我将调用上述文件进行数据验证的脚本代码。

<script type="text/javascript">
    $(document).ready(function()    //When the dom is ready 
    
    $("#uname").change(function() 
     //if theres a change in the uname textbox

    var uname = $("#uname").val();    //Get the value in the uname textbox
    //var re = ^([\w-]+(?:\.[\w-]+)*)\$;
    if(uname) //if the length greater than 5 characters
    
    $("#availability_status").html('<img src="/ahs/images/loader.gif" align="absmiddle">&nbsp;Checking availability...');
    //Add a loading image in the span id="availability_status"

    $.ajax(  //Make the Ajax Request
        type: "POST",  
        url: "/abc/app/views/ControlPanelViews/forms/ajax_check_uname.php",  //file name
        data: "uname="+ uname,  //data
        //data: uname: value,
        success: function(server_response)  

       $("#availability_status").ajaxComplete(function(event, request) 

        if(server_response == '0')  //if ajax_check_username.php return value "0"
         
        $("#availability_status").html('<img src="/abc/images/available.png" align="absmiddle"> <font color="Green"> Available </font>  ');
        //add this image to the span with id "availability_status"
          
        else  if(server_response == '1')    //if it returns "1"
          
         $("#availability_status").html('<img src="/abc/images/not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
           
       );
        
      ); 
    

    else
    

    $("#availability_status").html('<label></label>');
    //if in case the uname is less than or equal 6 characters only 
    


    return false;
    );

    );
</script>

以下是显示数据的 HTML 表单代码。

HTML 表格:

<div class="form-group">
<label class="control-label col-md-3">Username</label>
<div class="col-md-9">
<input type="text" name="uname" id="uname" class="form-control" placeholder="name" type="text">
<span id="availability_status"></span>
</div></div>

【问题讨论】:

【参考方案1】:
<script type="text/javascript">
    $(document).ready(function()    //When the dom is ready 
    
    $("#uname").change(function() 
     //if theres a change in the uname textbox

    var uname = $("#uname").val();    //Get the value in the uname textbox
    //var re = ^([\w-]+(?:\.[\w-]+)*)\$;
    if(uname) //if the length greater than 5 characters
    
    $("#availability_status").html('<img src="/ahs/images/loader.gif" align="absmiddle">&nbsp;Checking availability...');
    //Add a loading image in the span id="availability_status"

    $.ajax(  //Make the Ajax Request
        type: "POST",  
        url: "/abc/app/views/ControlPanelViews/forms/ajax_check_uname.php",  //file name
        data: uname: uname,  //data
        success: function(server_response)  

       $("#availability_status").ajaxComplete(function(event, request) 

        if(server_response == '0')  //if ajax_check_username.php return value "0"
         
        $("#availability_status").html('<img src="/abc/images/available.png" align="absmiddle"> <font color="Green"> Available </font>  ');
        //add this image to the span with id "availability_status"
          
        else  if(server_response == '1')    //if it returns "1"
          
         $("#availability_status").html('<img src="/abc/images/not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
           
       );
        
      ); 
    

    else
    

    $("#availability_status").html('<label></label>');
    //if in case the uname is less than or equal 6 characters only 
    


    return false;
    );

    );
</script>

【讨论】:

以上是关于无法从 js 文件中获取数据的主要内容,如果未能解决你的问题,请参考以下文章

Dropzone.js 无法从 HTML 编辑器 CKEDITOR 中获取值

从json文件获取数据到Nodejs(Protractor代码)

为啥 PHP 无法从 Js FormData fetch 中获取 $_POST 数据?

如何从 mongodb 获取数据并使用节点 js 将其显示在表中?

无法从 v-for 获取数据且数据为空

无法使用 Angular js 2 打印数据