SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Got encountered problem in ajax by stopped process but no error

    Hi..

    I have ajax code for saving data to database
    here is my ajax code:

    Code:
    <script type="text/javascript">
    var ajaxTimeOut = null;
    var ajaxTimeOutOperator = null; 
    var responsePHP; // = "no_reply"
    var responsePHPOperator;
    var changeFocus; //= false;
    var transactionWasSaved;
    
    function remoteRequestObject() {
        var ajaxRequest = false;
        try {
            ajaxRequest = new XMLHttpRequest();
        }
        catch(err) {
            try{
                ajaxRequest = new ActiveXObject("MSxml2.XMLHTTP");
            }
            catch(err) {
                try{
                    ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(err){
                    // --> change to DOM alert("Not Supported Browser") + err.description;
                    notify('Not Supported Browser.');
                    return false;
                }
            }
        }
        return ajaxRequest;
    } 
    
    //var ajaxRequest; // = remoteRequestObject();
    var ajaxRequest = remoteRequestObject();   
    var ajaxRequestOperator;
    </script>
    
    <script type="text/javascript">
    function lotIdCheck(element) {
        if (window.event.keyCode==13 || window.event.keyCode==10) {
            
            var txtElementID = element;
            if (txtElementID.value == "") { return false; }
           // alert(txtElementID.value);
         
                        if (ifMatch(txtElementID.value, document.getElementById('txtLotCode').value)) {
                           capture(element);
                        }
                        else {
                           
                           txtElementID.value = "";
                           txtElementID.focus();
                           notify("Lot ID mismatch.Scan again"); //box instead of this... 
                           
           
            }        
        }    
    }
    
    
    function timeOut(object) { 
    
      if (ajaxTimeOut != null) {
          window.clearTimeout(ajaxTimeOut);   
      }    
      //alert("this is the reponse " + responsePHP)
       if (changeFocus = false || responsePHP == "no_reply" || ajaxRequest.readyState != 4 && ajaxRequest.readyState != 0) {
          //alert('ajaxRequest not ready');
          ajaxRequest.onreadystatechange = function() {};   
          ajaxRequest.abort();
          document.getElementById(object).disabled = false;
          document.getElementById(object).value = "";
          changeInputType(document.getElementById(object),'password');
     
          document.getElementById(object).focus();
          document.getElementById(object).focus();
          
          var txt = document.getElementById(object);
          if (txt.attachEvent) {
              txt.attachEvent ("onkeypress", function () {lotIdCheck(txt)});
          }  
          //notify("Please Scan again.");  
          alert("Please Scan again.");
          
       }
       else {
          return false;
       }
    }
    
    function timeOutOperator() { 
    
    if (ajaxTimeOutOperator !== null) {
          window.clearTimeout(ajaxTimeOutOperator);   
      }    
    
    if (transactionWasSaved = false || responsePHPOperator == "no_reply" || ajaxRequestOperator.readyState != 4 && ajaxRequestOperator.readyState != 0) {
          //alert('ajaxRequest not ready');
          ajaxRequestOperator.onreadystatechange = function() {};   
          ajaxRequestOperator.abort();
          document.getElementById('txtLotCode').disabled = false;
          document.getElementById('txtLotCode').value = "";
          document.getElementById('txtLotCode').focus();
          document.getElementById('txtLotCode').focus();
          //notify("Please Scan again.");  
          alert("Please Scan again.");   
    }
       else {
          return false;
       }
    }
    
    
    function clearTiming() {
        changeFocus = true;
        if (ajaxTimeOut !== null) {
          window.clearTimeout(ajaxTimeOut);   
      }  
    }
    
    function clearTimingOperator() {
        transactionWasSaved = true;
        if (ajaxTimeOutOperator !== null) {
          window.clearTimeout(ajaxTimeOutOperator);   
      }  
    }
    
    function postSet() {
        if (window.event.keyCode==13 || window.event.keyCode==10) {
            document.getElementById('txtLotCode').disabled = true;
            
            verifyLotcode();
        }
    }
    
    //------------------------------------------------------------------------------------------------------------------
    
    function verifyLotcode() {
    responsePHPOperator = "no_reply";
    transactionWasSaved = false;
    ajaxRequestOperator = remoteRequestObject();
    ajaxRequestOperator.onreadystatechange = function () {
    if (ajaxRequestOperator.readyState==4 && ajaxRequestOperator.status==200) {
         //var result = ajaxRequestOperator.responseText;
         
         responsePHPOperator = ajaxRequestOperator.responseText; 
         
       //  alert(responsePHPOperator);
         if (responsePHPOperator == "failed") {
            document.getElementById('txtLotCode').disabled = false;
            document.getElementById('txtLotCode').value = "";
            document.getElementById('txtLotCode').focus();
           // notify("Please scan again.");
           alert("Please scan again.");
         }
         
         if (responsePHPOperator == "0") {
             
         //->> change to DOM    alert("Unauthorized operator");
            // notify("Unauthorized operator.<br />Scan Again.");
            alert("Unauthorized operator.<br/>Scan Again.");
             if (document.getElementById('txtLotCode').disabled) {
                 document.getElementById('txtLotCode').disabled = false; 
             } 
             document.getElementById('txtLotCode').value = "";
             document.getElementById('txtLotCode').focus();
         }
         else { 
            // alert ('correct');
             save();                
           //  notify("Transaction has been saved.");
           //  reset();
         }    
       }
    } 
    
    var str = document.getElementById('txtLotCode').value;
    var url = "validate_lotcode.php?q="+str;
    ajaxRequestOperator.open("GET",url , true );           
    
    // force i.e. not to cache
    ajaxRequestOperator.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    //---------------------------
    ajaxRequestOperator.send(null);
    ajaxTimeOutOperator = window.setTimeout("timeOutOperator();", 100000000);              
    }
    
    function save() {
     // ajaxRequest = remoteRequestObject();              
        ajaxRequest.onreadystatechange = function () {
        if (ajaxRequest.readyState==4 && ajaxRequest.status==200) {
           var result = ajaxRequest.responseText;
           
           // alert (result);
              
               if (result == "failed") {
                 // alert (result);
                document.getElementById('txtLotCode').disabled = false;
                document.getElementById('txtLotCode').value = "";
                document.getElementById('txtLotCode').focus();
                //notify("Please scan again.");
               alert("Please scan again");
               }
               
               if (result == "saved|saved" || result == "saved|saved|saved") {
                  //alert(result);
                 alert("Transaction has been saved.");
                  //notify("Transaction has been saved.");
                //  reset();
               }   
               
           }     
        }      
     
                 var parameters = "txtLotCode=" + encodeURI(document.getElementById('txtLotCode').value); 
    
     var url = "update_dependency.php";  
     
    
    ajaxRequest.open("POST", url, true);
    ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajaxRequest.setRequestHeader("Content-length", parameters.length);
    ajaxRequest.setRequestHeader("Connection", "close");
    ajaxRequest.send(parameters);
    }
    </script>
    
    
    <script type="text/javascript">
    function reset() {
            // alert ('reset');
           //enable elements
           
           document.getElementById('txtLotCode').disabled = false;
           
           //reset
      
      document.getElementById('txtLotCode').value = "";  
    }
    </script>
    I got problem in function save, even it satisfied this code:

    Code:
    if (result == "saved|saved" || result == "saved|saved|saved") {
    but it did not post alert and also not go to reset function.


    I don't know why?

    Thank you

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by newphpcoder View Post
    I got problem in function save, even it satisfied this code:

    Code:
    if (result == "saved|saved" || result == "saved|saved|saved") {
    but it did not post alert and also not go to reset function.
    If that test passes, you must get the alert. When earlier you alert result, presumably it appears to contain "saved|saved" || result == "saved|saved|saved", but are you sure the case matches and there are no boundary whitespace characters?
    I think it would be better to use a regex here.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it alerts the :

    saved|saved|saved
    but the next alert :

    alert("Transaction has been saved."); was not

    and also it save the data and the textbox was disabled that was happened.it did not display the alert :
    alert("Transaction has been saved."); and it did not cleared the textbox

    How can I use regex?

    Thank you

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by newphpcoder View Post
    it alerts the :

    saved|saved|saved
    but the next alert :

    alert("Transaction has been saved."); was not
    Try alert( ">" + result + "<" );

    You should see

    >saved|saved|saved<

    not

    >saved|saved|saved <

    or similar.

    Use the test:
    Code:
    if ( /(^|\s)saved\|saved(\|saved)?(\s|$)/.test( result )  )
    Tab-indentation is a crime against humanity.

  5. #5
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi..

    I tried your suggestion and the result is when I input the lotcode and I press the enter key the textbox was disabled then nothing was happened next no alerts displayed

    Thank you

  6. #6
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by newphpcoder View Post
    Hi..

    I tried your suggestion and the result is when I input the lotcode and I press the enter key the textbox was disabled then nothing was happened next no alerts displayed
    Any console errors?

    Can you host a live demo?
    Tab-indentation is a crime against humanity.

  7. #7
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    657
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi..

    Resolved it using this code:

    Code:
             if ( result.toLowerCase().substring(0,4) == "save" )  {
    
                  //alert(result);
                  //alert( ">" + result + "<" );
                //alert("Transaction has been saved.");
                 notify("Transaction has been saved.");
                  reset();
               }
    now..
    my function notify() did not display the notification message but no error:

    here is my code:

    Code:
    function notify(message) {
        if (document.getElementById('notice') !== null) {
            document.getElementsByTagName('body')[0].removeChild(document.getElementById('notice')) //>> clear the box 
        } 
        alerter('dunlop1.jpg','Kanban Withdrawal',message);
        
    }
    
    function clearNotify() {
        
       if (document.getElementById('notice') != null) {
          document.getElementsByTagName('body')[0].removeChild(document.getElementById('notice')) //>> clear the box    } 
       }
    }
    Code:
    /*************************** below code can be in a js file  *****************************/
    var response = null 
    function alerter(promptpicture, prompttitle, message) { 
        notifybox = document.createElement('div'); 
        notifybox.setAttribute ('id' , 'notice') 
        document.getElementsByTagName('body')[0].appendChild(notifybox) 
        notifybox = eval("document.getElementById('notice').style") 
        notifybox.position = 'absolute' 
        //var termNo = terminalNo();
       // if (termNo == '1' || termNo == '2' || termNo == '3') {
       //     notifybox.top = 380 //320
       // }
        
        //if (termNo == '4' || termNo == '5' || termNo == '6' || termNo == '7') {
       //     notifybox.top = 420 //360
      //  }
    //=========================================================================    
       // if (message == 'Invalid Number.<br />Input Again.' || message == 'Exceed Value.<br />Input Again.' || message == 'Invalid clear tag') {
      //  if (document.getElementById('prompt') !== null) {
           // notifybox.width = '180px'
            //var offSetValueLeft = (document.body.offsetWidth / 2) - (document.getElementById('notice').offsetWidth)
      //  }
      //  else {
           // notifybox.width = '350px'
        //    var offSetValueLeft = (document.body.offsetWidth / 2) - (document.getElementById('notice').offsetWidth / 2)
       // }
    //=========================================================================    
        if (document.getElementById('prompt') !== null) {
          notifybox.width = '180px'
            var offSetValueLeft = (document.body.offsetWidth / 2) - (document.getElementById('notice').offsetWidth)
        }
        else {
            notifybox.width = '350px'
            var offSetValueLeft = (document.body.offsetWidth / 2) - (document.getElementById('notice').offsetWidth / 2)
        }
    //=========================================================================    
    
        
        
        notifybox.left = offSetValueLeft  
     //    promptbox.height = '96px'
     //   promptbox.border = '2 solid #000000'
     //    promptbox.color= '#0000F2'
     //    promptbox.background-color= '#FFFFCC' 
        notifybox.border = 'outset 1 #bbbbbb' 
    
        document.getElementById('notice').innerHTML = "<table cellspacing='0' cellpadding='0' border='0' width='100%'><tr valign='middle'><td width='22' height='22' style='text-indent:2;' class='ntitlebar'><img src='" + promptpicture + "' height='18' width='18'></td><td class='titlebar'>" + prompttitle + "</td></tr></table>" 
    
        document.getElementById('notice').innerHTML = document.getElementById('notice').innerHTML + "<table cellspacing='0' cellpadding='0' border='0' width='100%' class='notifybox'><tr><td align='center'><br /><h2>" + message + "</h2></td></tr><tr><td></td></tr></table>" 
    
    
     //   document.getElementById('notice').innerHTML = document.getElementById('notice').innerHTML + "<table><p style='font-family: Arial, Helvetica, sans-serif; font-weight:bold;'><center><br><br><h2 id='midText'>"  + message + "</h2></center></p>" 
    } 
            /***************************  js file code *****************************/
    Thank you


Bookmarks

Posting Permissions

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