www.webdeveloper.com
Results 1 to 3 of 3

Thread: AJAX:Problem in responseText if condition equal to responseText but fall to else

  1. #1
    Join Date
    Oct 2011
    Posts
    350

    AJAX:Problem in responseText if condition equal to responseText but fall to else

    Hi,

    Good day!

    I encountered problem on my ajax code. I think it my responsetext has a whitespace so that it falls on my else statement.

    here is my ajax code:

    Code:
    //----barcode---//
    function get_data(){
        if (window.event.keyCode==13 || window.event.keyCode==10) {
        var barcode_wms = document.getElementById("barcode").value;
       // var divid = 'display_data';
        var url = "getbarcode_data.php";
        var str = "mc=" + barcode_wms;
    
        var xmlHttp = AJAX();
    
       // alert(xmlHttp);
        xmlHttp.onreadystatechange=function(){
        //alert("test");
       /*
        if(xmlHttp.readyState > 0 && xmlHttp.readyState < 4){
          //  document.getElementById(divid).innerHTML=loadingmessage;
            }
        */    
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            //alert('test');
                var jsonart = xmlHttp.responseText;
                //jsonart = jsonart.Itrim()
                alert(jsonart);
                if(jsonart==="Invalid")
                {
                 alert("Invalid Barcode");
                    //notify("Invalid Barcode");
                 //refresh(1500);
                }
                else
                {
                    alert("Correct Barcode");
                /*    
                var strsplit = jsonart.split("^");
                document.getElementById("po_number").value = strsplit[0];
                document.getElementById("unique_id").value = strsplit[1];
                document.getElementById("kind").value = strsplit[2];
                document.getElementById("material_code").value = strsplit[3];
                document.getElementById("description").value = strsplit[4] ;
                document.getElementById("uom").value = strsplit[5] ;
                document.getElementById("supplier").value = strsplit[6] ;           
                document.getElementById("qty").value = strsplit[7] ;
                }
                */
                }
            //}
        }
        }
        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.setRequestHeader("Content-length", str.length);
        xmlHttp.setRequestHeader("Connection", "close");
        xmlHttp.send(str);
       // }
    }
    }
    get_barcode_data.php

    PHP Code:
    <?php
    include("includes/connection.php");
    //if($_POST["mc"])
       // {
        
    $barcode $_POST['mc'];
        
    $po_number substr($barcode05);  
        
    $item_code substr($barcode56);  
        
    $supplier substr($barcode113);  
        
    $uom substr($barcode142); 
        
    $unique_id substr($barcode166); 
        
    $qty substr($barcode22);   
        
    $query "SELECT mat_id, supplier_id, uom_id ";
        
    $query .= "FROM check_info ";
        
    $query .= "WHERE mat_id = '$item_code' AND supplier_id = '$supplier' AND uom_id = '$uom'";
        
    $result mysql_query($query);
        
    $cnt mysql_num_rows($result);
        if(
    $cnt==0)
        {
            echo(
    "Invalid");
        }
        else
        {
            echo(
    "Correct");
        }
        exit;
    and the index.php
    PHP Code:
    <?php
        
    include('includes/connection.php');
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css"  href="css/wms.css">
    <script type="text/javascript" src="javascript/wms.js"> </script>
    <script type="text/javascript" src="javascript/prompt.js"> </script>
    <script type="text/javascript" src="javascript/notification.js"> </script>

    <link rel="stylesheet" type="text/css"  href="css/prompt.css">
    <link rel="stylesheet" type="text/css"  href="css/notify.css">
    <title>Warehouse Barcoding</title>
    </head>
    <body>
    <form name="wms" action="" method="post">
    <?php
        
    echo "<div>";
        echo 
    "<label> Material Barcode: </label>";
        echo 
    "<input type='password' name='barcode' id='barcode' value='' onkeypress='get_data();' size='40'>";
        echo 
    "</div>";
    //$barcode = '10001CHE00102401100200250000.50';
        
      
      /*
        echo "<div id='display_data'>";
        echo "<table>";
        echo "<tr>";
        echo "<th>PO Number</th>";
        echo "<th>Unique ID</th>";
        echo "<th>Kind</th>";
        echo "<th>Item Code</th>";
        echo "<th>Description</th>";
        echo "<th>UoM</th>";
        echo "<th>Supplier</th>";
        echo "<th>Quantity</th>";
        echo "</tr>";
        
        echo "<tr>";
        echo "<td><input type='text' name='po_number' id='po_number' value='' style=' border: none;
        background: transparent;'></td>";
        echo "<td><input type='text' name='unique_id' id='unique_id' value='' style=' border: none;
        background: transparent;'></td>";
        echo "<td><input type='text' name='kind' id='kind' value='' style=' border: none;
        background: transparent;'></td>";
        echo "<td><input type='text' name='material_code' id='material_code' value='' style=' border: none;
        background: transparent;'></td>";
        echo "<td><input type='text' name='description' id='description' value='' style=' border: none;
        background: transparent;'></td>";
        echo "<td><input type='text' name='uom' id='uom' value='' style=' border: none;
        background: transparent;'></td>";
        echo "<td><input type='text' name='supplier' id='supplier' value='' style=' border: none;
        background: transparent;'></td>";    
        echo "<td><input type='text' name='qty' id='qty' value='' style=' border: none;
        background: transparent;'></td>";
        echo "</tr>";
        echo "</table>";    
        echo "</div>";    
       */
    ?>
    </form>
    </body>
    </html>
    I can't figured out what is the cause why it did not fall into if condition even the value of responsetext is Invalid.


    I hope somebody can help me.

    Thank you.

  2. #2
    Join Date
    Aug 2013
    Posts
    30
    have you searched other resources about the barcode code ?

  3. #3
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    862
    You should use console.log() instead of alert() when debugging, especially if you are concerned about potential whitespace. It's not really possible to see things like that in alert() boxes, however in the console everything is printed out in a much more readable form that better allows you to see (and debug) variables.

    Other than that, since I can't actually run your code I would just have to ask about using the strict comparison operator (===) vs the more common and standard equals operator (==). Logically you shouldn't need the strict comparison since you probably won't have to worry about types in this instance.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles