Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts

    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
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    There's often an issue when comparing strings that were delivered from the server. It's best practice to remove all additional whitespace from it first
    Code:
    jsonart = jsonart.replace(/^\s+|\s+$/g, "");

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    There's often an issue when comparing strings that were delivered from the server. It's best practice to remove all additional whitespace from it first
    Code:
    jsonart = jsonart.replace(/^\s+|\s+$/g, "");
    Code:
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            //alert('test');
                var jsonart = xmlHttp.responseText;
                jsonart = jsonart.replace(/^\s+|\s+$/g, "");
                alert(jsonart);
                if(jsonart=="Invalid")
                {
                 alert("Invalid Barcode");
                    //notify("Invalid Barcode");
                 //refresh(1500);
                }
                else
                {
                    alert("Correct Barcode");
                }
    Thank you

    I tried this Sir, still the jsonart alert = 'Invalid' ,but also it alert the "Correct Barcode". instead of "Invalid Barcode"

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    713
    Thanks
    165
    Thanked 0 Times in 0 Posts
    Hi Sir,

    I tried this:

    Code:
    var loadingmessage = '<center>Getting material info ... <img src="images/loading.gif" /></center>';
    function AJAX(){
        var xmlHttp;
        try{
            xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
            return xmlHttp;
            }
        catch (e){
            try{
                xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
                return xmlHttp;
                }
            catch (e){
                try{
                    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
                    return xmlHttp;
                    }
                catch (e){
                    alert("Your browser does not support AJAX!");
                    return false;
                    }
                }
            }
        }
        
    function notify(message) {
        if (document.getElementById('notice') !== null) {
            document.getElementsByTagName('body')[0].removeChild(document.getElementById('notice')) //>> clear the box
        }
        alerter('images/dunlop1.jpg','Warehouse Management System',message);
    
    }
    
    function refresh(time){
     setTimeout("location.reload(true);",time);
    }
    
    //----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.replace(/^\s+|\s+$/g, "");
                alert(jsonart);
                if (jsonart.replace(/\s+/g, '') == "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);
       // }
    }
    }
    It falls to If condition now the alert is Invalid Barcode but it did not display the Notification from notify function.

    I have no clue it did not display notification

    Thank you

  • #5
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,395
    Thanks
    12
    Thanked 569 Times in 562 Posts
    What does the function alerter() do? You are calling it in notify() ...


  •  

    Posting Permissions

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