Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-03-2012, 08:48 AM   PM User | #1
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
'ajaxRequest' is null or not an object

Hi....

I have form which I put save function on the last textbox:
here is my code:

Code:
<?php
    error_reporting(0);
   date_default_timezone_set("Asia/Singapore"); //set the time zone  
$con = mysql_connect('localhost', 'root','');

if (!$con) {
    echo 'failed';
    die();
}
mysql_select_db("mes", $con);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<script type="text/javascript" >  

var input_size = 1;

function checkTextBox(bc){
   var barcode_ =  bc.tabIndex;
   
   if ( bc.value.length > input_size ) 
        { 
            for(i=0; i<document.barcode.elements.length; i++) 
            { 
                      if( document.barcode.elements[i].tabIndex == (barcode_+1) ) 
                     {    
                         document.barcode.elements[i].focus(); 
                         break; 
                     } 
          } 
    }         
} 

function postSet() {
    if (window.event.keyCode==13 || window.event.keyCode==10) {
        document.getElementById('code_read_box6').disabled = true;
        save();
        alert('code_read_box6');
    }
}
 
</script> 

<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 ajaxRequestOperator;
</script>

<script type="text/javascript">
function save() {            
    ajaxRequest.onreadystatechange = function () {
    if (ajaxRequest.readyState==4 && ajaxRequest.status==200) {
       var result = ajaxRequest.responseText;
       
        alert (result);
           
           if (result == "failed") {
            document.getElementById('code_read_box6').disabled = false;
            document.getElementById('code_read_box6').value = "";
            document.getElementById('code_read_box6').focus();
            notify("Please scan again.");
           }
           
           if (result == "saved") {
              alert(result);
              notify("Transaction has been saved.");
              reset();
           }   
           
       }     
    }      
   
    
 var url = "save_barcode.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> 
 
</head> 
<body onLoad="document.barcode.code_read_box1.focus();"> 
<form name="barcode" > 
<input type="text" tabindex="1" id="code_read_box1" value="" onkeyup="checkTextBox(this);"/><br/> 
<input type="text" tabindex="2" id="code_read_box2" value="" onkeyup="checkTextBox(this);"/><br/> 
<input type="text" tabindex="3" id="code_read_box3" value="" onkeyup="checkTextBox(this);"/><br/> 
<input type="text" tabindex="4" id="code_read_box4" value="" onkeyup="checkTextBox(this);"/><br/> 
<input type="text" tabindex="5" id="code_read_box5" value="" onkeyup="checkTextBox(this);"/><br/> 
<input type="text" tabindex="6" id="code_read_box6" value="" onkeyup="checkTextBox(this);" onkeypress="postSet()"/><br/> 
</form> 
</body> 
 
</html>
I got an error:
'ajaxRequest' is null or not an object on line 72

It display the error when I press enter on the last textbox.

Sorry, I'm not familiar in ajax..I hope somebody can help me

Thank you
newphpcoder is offline   Reply With Quote
Old 05-03-2012, 10:08 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
This line
Code:
var ajaxRequest; // = remoteRequestObject();
will create the ajaxRequest variable but it will still be undefined. Why did you comment the rest of the line? You need to assign remoteRequestObject() to ajaxRequest otherwise it won't work
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
newphpcoder (05-04-2012)
Old 05-04-2012, 02:18 AM   PM User | #3
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
Thank you it resolves my error:, but when I run my code I got a new error:

error: 'parameters' is undefined on line 121

Code:
<?php
    error_reporting(0);
   date_default_timezone_set("Asia/Singapore"); //set the time zone 
$con = mysql_connect('localhost', 'root','');

if (!$con) {
    echo 'failed';
    die();
}
mysql_select_db("mes", $con);
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" > 

var input_size = 1;

function checkTextBox(bc){
   var barcode_ =  bc.tabIndex;
  
   if ( bc.value.length > input_size )
        {
            for(i=0; i<document.barcode.elements.length; i++)
            {
                      if( document.barcode.elements[i].tabIndex == (barcode_+1) )
                     {   
                         document.barcode.elements[i].focus();
                         break;
                     }
          }
    }        
}

function postSet() {
    if (window.event.keyCode==13 || window.event.keyCode==10) {
        document.getElementById('code_read_box6').disabled = true;
        save();
        alert('code_read_box6');
    }
}
 
</script>

<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 reset(){
                       document.getElementById('code_read_box1').disabled = false;
                       document.getElementById('code_read_box2').disabled = false;
                       document.getElementById('code_read_box3').disabled = false;
                       document.getElementById('code_read_box4').disabled = false;
                       document.getElementById('code_read_box5').disabled = false;
                       document.getElementById('code_read_box6').disabled = false;
                      
                       //reset
                       document.getElementById('code_read_box1').value = "";
                       document.getElementById('code_read_box2').value = "";
                       document.getElementById('code_read_box3').value = "";
                       document.getElementById('code_read_box4').value = "";
                       document.getElementById('code_read_box5').value = "";
                       document.getElementById('code_read_box6').value = "";
                      
                      
}
</script>

<script type="text/javascript">
function save() {           
    ajaxRequest.onreadystatechange = function () {
    if (ajaxRequest.readyState==4 && ajaxRequest.status==200) {
       var result = ajaxRequest.responseText;
      
        alert (result);
          
           if (result == "failed") {
            document.getElementById('code_read_box6').disabled = false;
            document.getElementById('code_read_box6').value = "";
            document.getElementById('code_read_box6').focus();
            notify("Please scan again.");
           }
          
           if (result == "saved") {
              alert(result);
              notify("Transaction has been saved.");
              reset();
           }  
          
       }    
    }     
  
   
 var url = "save_barcode.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>
 
</head>
<body onLoad="document.barcode.code_read_box1.focus();">
<form name="barcode" >
<input type="text" tabindex="1" id="code_read_box1" value="" onkeyup="checkTextBox(this);"/><br/>
<input type="text" tabindex="2" id="code_read_box2" value="" onkeyup="checkTextBox(this);"/><br/>
<input type="text" tabindex="3" id="code_read_box3" value="" onkeyup="checkTextBox(this);"/><br/>
<input type="text" tabindex="4" id="code_read_box4" value="" onkeyup="checkTextBox(this);"/><br/>
<input type="text" tabindex="5" id="code_read_box5" value="" onkeyup="checkTextBox(this);"/><br/>
<input type="text" tabindex="6" id="code_read_box6" value="" onkeyup="checkTextBox(this);" onkeypress="postSet()"/><br/>
</form>
</body>
 
</html>


Thank you
newphpcoder is offline   Reply With Quote
Old 05-04-2012, 06:22 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,857
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
that’s because you never defined this variable.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 05-04-2012, 06:24 AM   PM User | #5
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
that’s because you never defined this variable.
what variable?

Thank you
newphpcoder is offline   Reply With Quote
Old 05-04-2012, 06:47 AM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,857
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by newphpcoder View Post
what variable?
the one mentioned in the error message: parameters
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 05-07-2012, 01:44 AM   PM User | #7
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
the one mentioned in the error message: parameters
I can't figured out what parameter it means

Thank you
newphpcoder is offline   Reply With Quote
Old 05-07-2012, 06:14 AM   PM User | #8
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,857
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
didn’t you write the code?
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 05-08-2012, 01:36 AM   PM User | #9
newphpcoder
Regular Coder

 
Join Date: Aug 2010
Posts: 653
Thanks: 155
Thanked 0 Times in 0 Posts
newphpcoder is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
didn’t you write the code?
No

Thank you
newphpcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:59 PM.


Advertisement
Log in to turn off these ads.