newphpcoder Posted May 3, 2012 Share Posted May 3, 2012 Hi.... I have form which I put save function on the last textbox: here is my 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 Quote Link to comment https://forums.phpfreaks.com/topic/261995-ajaxrequest-is-null-or-not-an-object/ Share on other sites More sharing options...
trq Posted May 3, 2012 Share Posted May 3, 2012 Sorry, I'm not familiar in ajax You meen Javascript. You don't define ajaxRequest anywhere before trying to use like it's an object. This line: var ajaxRequest; // = remoteRequestObject(); Should be.... var ajaxRequest = remoteRequestObject(); Quote Link to comment https://forums.phpfreaks.com/topic/261995-ajaxrequest-is-null-or-not-an-object/#findComment-1342614 Share on other sites More sharing options...
newphpcoder Posted May 4, 2012 Author Share Posted May 4, 2012 Thank you it resolves my error:, but when I run my code I got a new error: error: 'parameters' is undefined on line 121 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/261995-ajaxrequest-is-null-or-not-an-object/#findComment-1342889 Share on other sites More sharing options...
trq Posted May 4, 2012 Share Posted May 4, 2012 These types of errors are self explanatory. Your trying to use variables that haven't been defined. Quote Link to comment https://forums.phpfreaks.com/topic/261995-ajaxrequest-is-null-or-not-an-object/#findComment-1342903 Share on other sites More sharing options...
newphpcoder Posted May 4, 2012 Author Share Posted May 4, 2012 the result variable? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/261995-ajaxrequest-is-null-or-not-an-object/#findComment-1342904 Share on other sites More sharing options...
trq Posted May 4, 2012 Share Posted May 4, 2012 The error message tells you exactly what variable is undefined. Quote Link to comment https://forums.phpfreaks.com/topic/261995-ajaxrequest-is-null-or-not-an-object/#findComment-1342960 Share on other sites More sharing options...
newphpcoder Posted May 7, 2012 Author Share Posted May 7, 2012 I can't figured out what variable was undefined Thank you Quote Link to comment https://forums.phpfreaks.com/topic/261995-ajaxrequest-is-null-or-not-an-object/#findComment-1343587 Share on other sites More sharing options...
The Letter E Posted May 14, 2012 Share Posted May 14, 2012 Quote Link to comment https://forums.phpfreaks.com/topic/261995-ajaxrequest-is-null-or-not-an-object/#findComment-1345235 Share on other sites More sharing options...
mrMarcus Posted May 17, 2012 Share Posted May 17, 2012 The error is referring to 'parameters' not being defined anywhere in your code: 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); What your function save() is supposed to do, by the looks of things, is save data to a database (I'm merely assuming that's what happens in save_barcode.php). 'parameters' would be a set of, well, parameters that contain values based on what your form is doing, and the overall intentions of your application. I have no idea what your application is doing to be honest, and I'm wondering the same about you. parameters should be something along the following, but obviously specific to what you're trying to achieve and based off values from your form (perhaps?): var parameters = 'foo1=bar1&foo2=bar2'; Those values will be sent to 'save_barcode.php' to be processed. Quote Link to comment https://forums.phpfreaks.com/topic/261995-ajaxrequest-is-null-or-not-an-object/#findComment-1346423 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.